# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="1GFF0jx_SPA6" outputId="08dbaffa-9bdc-4fb9-a0c9-cae1f38a4910"
'''from google.colab import drive
drive.mount('/content/drive')'''

# + colab={"base_uri": "https://localhost:8080/", "height": 105} colab_type="code" id="VcC41lLISYvV" outputId="cd6a6a73-9b21-4043-8315-81e346270dfc"
pip install split-folders

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="RX2v4kHiSbR0" outputId="27ed09f1-8035-4eff-db51-2185c6e3ed70"
import numpy as np
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
from keras.layers import Input, Lambda, Dense, Flatten
from keras.models import Model
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
import split_folders
import glob
import os
from PIL import Image
import matplotlib.pyplot as plt

from keras.models import model_from_json
import pickle
import math

# + colab={} colab_type="code" id="wFYNOF1FS7xX"
driveZip = '/content/drive/My Drive/temp/cropped_images.zip'

image_types = ["kurti","saree","none","shirt","tshirt"]
batch_size = 32
image_size = [224,224]
num_classes = 4

# + colab={} colab_type="code" id="oOsehSelUDha"
import zipfile
with zipfile.ZipFile(driveZip,'r') as zip_dir:
    zip_dir.extractall(path='/content/')

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="KE0Fb_DZUFhB" outputId="93b47b51-dcc9-49c6-bd99-8683df2d789c"
split_folders.ratio('cropped_images', output="output", seed=1337, ratio= (.8,.2)) # default values


# + colab={} colab_type="code" id="weh6Ro90ULLw"
# Image size should be [224,224]
# Iterate through each color folder

def resize_images(image_dir):
  for im_type in image_types:
    # Iterate through each image file in each image_type folder
    #  glob reads in any image with the extension "image_dir/im_type/*"
    for file in glob.glob(os.path.join(image_dir, im_type, "*")):
        im = Image.open(file)
        f, e = os.path.splitext(file)
        imResize = im.resize((224,224), Image.ANTIALIAS)
        imResize.save(f + '.png', 'PNG', quality=90)

resize_images('/content/output/val')

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="F57Xbzkvagk8" outputId="a1345b1a-d95a-4379-8aba-ef7c89fb4cb7"
# !rm -r /content/output/train

# !rm -r NOA
# !mkdir NOA

# !mkdir NOA/val
# !mkdir NOA/val/none
# !cp -r '/content/output/val/none/' '/content/NOA/val/'
# !rm -r /content/output/val/none

# + colab={} colab_type="code" id="SLX7w699apo1"
# !cp '/content/drive/My Drive/temp/VGG/secondLastModel.json' '/content/'
# !cp '/content/drive/My Drive/temp/VGG/secondLastModel.h5' '/content/'

# !cp '/content/drive/My Drive/temp/VGG/lastModel.json' '/content/'
# !cp '/content/drive/My Drive/temp/VGG/lastModel.h5' '/content/'

# !cp '/content/drive/My Drive/temp/VGG/logRModel.sav' '/content/'

# + colab={"base_uri": "https://localhost:8080/", "height": 901} colab_type="code" id="FIO7HOzrbKUz" outputId="3eb0d565-0d2e-43fa-8ab3-bf90e3b0a263"
json_file = open('secondLastModel.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
secondLastModel = model_from_json(loaded_model_json)

secondLastModel.load_weights("secondLastModel.h5")
secondLastModel.summary()

# + colab={} colab_type="code" id="xA7oFJskbyha"
val_datagen = ImageDataGenerator(rescale = 1./255)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="zLPdw5Ahb0gB" outputId="307eb94c-8033-45b9-fb51-1eb3a937b90f"
validation_set = val_datagen.flow_from_directory('/content/output/val/',
                                            target_size = (224, 224),
                                            batch_size = batch_size,
                                            class_mode = 'categorical')


# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="lrsa7PVYvtAd" outputId="0ba641c4-4094-4316-e97e-e487d8e427ab"
def getAllLabels(data_set):
    number_of_examples = len(data_set.filenames)
    number_of_generator_calls = math.ceil(number_of_examples / (1.0 * batch_size)) 
    # 1.0 above is to skip integer division
    test_labels = []
    for i in range(0,int(number_of_generator_calls)):
        test_labels.extend(np.array(data_set[i][1]))
    oneHotLabels = np.asarray(test_labels)
    labels = np.apply_along_axis(np.argmax,1, oneHotLabels)
    return labels

dtLabels = getAllLabels(validation_set)
print(dtLabels.shape)

# + colab={} colab_type="code" id="vInjx6RTb46z"
dtPredict = secondLastModel.predict_generator(validation_set,steps = len(validation_set))

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="Sdcb2Hr0cnwr" outputId="f7fdcb59-76bb-44a2-86b3-4a4ffb4c04e9"
print(dtPredict.shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="aymWQ_kxdegh" outputId="aad6e852-478f-409e-f94f-bdaa22327b6a"
labels = np.full(dtPredict.shape[0],1)
test1Set = np.concatenate((dtPredict,np.asarray([labels]).T), axis=1)
print(test1Set.shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="uDz3bl10dj3F" outputId="69cced1f-b5ae-48dd-f1bf-b505a48b534e"
validation_set = val_datagen.flow_from_directory('/content/NOA/val/',
                                            target_size = (224, 224),
                                            batch_size = batch_size)

# + colab={} colab_type="code" id="RkJD1rM6drds"
predict = secondLastModel.predict_generator(validation_set,steps = len(validation_set))

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="Xwah2EKOdv02" outputId="67ca54e1-6264-44d8-8642-5c557f466632"
labels = np.full(predict.shape[0],0,np.float32)
test0Set = np.concatenate((predict,np.asarray([labels]).T), axis=1)
print(test0Set.shape)


# + colab={} colab_type="code" id="DTawMtNXd0TN"
def predictNOA(testData):
    testX = testData[:,:-1]
    testY = testData[:,-1]
    print(testX.shape)
    print(testY.shape)

    clf = pickle.load(open('logRModel.sav', 'rb'))
    out = clf.predict(testX)
    return out

def getCount(x,val):
    return np.where(x == val)[0].shape[0]

def getCorectSamples(dataset,out,correctVal):
    newSet = []
    correctInds = np.where(out == correctVal)[0]
    for ind in correctInds:
        newSet.append(dataset[ind])
    return np.asarray(newSet)

def wrongLabelsCount(x,y):
    return np.where(x != y)[0].shape[0]


# + colab={} colab_type="code" id="i7jv5Ip_0czT"
totalSamples = test0Set.shape[0] + test1Set.shape[0]
wrongSamples = 0

# + colab={"base_uri": "https://localhost:8080/", "height": 51} colab_type="code" id="F0m7E-630XFn" outputId="af092e14-eda2-40aa-cfd4-35539f30e0a7"
out = predictNOA(test0Set)
wrongSamples += getCount(out,1)

# + colab={"base_uri": "https://localhost:8080/", "height": 51} colab_type="code" id="eZzWCy_T2xSn" outputId="b6d5d77f-37db-4fd1-f7bf-8daec61eded7"
out = predictNOA(test1Set)
wrongSamples += getCount(out,0)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="4160NS3z3BKb" outputId="52cf2e83-393d-45ee-e297-88918797f7f1"
newSet = getCorectSamples(test1Set,out,1)
print(newSet.shape)

# + colab={"base_uri": "https://localhost:8080/", "height": 221} colab_type="code" id="b-9ONoCY5h2L" outputId="030c2154-4ec2-4f2a-f752-f4f6398681ec"
json_file = open('lastModel.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
lastModel = model_from_json(loaded_model_json)

lastModel.load_weights("lastModel.h5")
lastModel.summary()

# + colab={} colab_type="code" id="fq191qp_50WR"
softMaxLabs = lastModel.predict(dtPredict)
predLabs = np.apply_along_axis(np.argmax,1, softMaxLabs)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="GtBSglNp9LVS" outputId="3c989a6a-5507-4817-dba9-91766067e2e4"
print(predLabs.shape,dtLabels.shape)

# + colab={} colab_type="code" id="PDB3wuMz7V6q"
wrongSamples += wrongLabelsCount(predLabs,dtLabels)

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" id="o0JtCBd897bM" outputId="17d29023-aca4-4850-a850-ca2a494ed11e"
print('Accuracy: ',(totalSamples - wrongSamples)/totalSamples)

StarcoderdataJupyterScriptsDedupFiltered