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

# + id="11La92D7sMoN"


# + id="5ddnT-ChTgyy" outputId="6b4b48af-dd1e-4a4b-81a9-21ee2b2479b4" colab={"base_uri": "https://localhost:8080/", "height": 34}
from google.colab import drive
drive.mount('/content/drive', force_remount=True)

# + id="-odkR3PcJu7u"
dir = "/content/drive/My Drive/InfraredSolarModules/"

# + id="1jwqCXRHTuDJ"
import numpy as np
import keras as k
import json
import PIL


# + id="ucFbuQLaUXU4"
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import VGG16 
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image


# + id="dDBPe4nNUC4U" outputId="894ac318-044b-4ff2-c462-1655f049c47b" colab={"base_uri": "https://localhost:8080/", "height": 1000}
def imagegen():
    f = open('/content/drive/My Drive/InfraredSolarModules/module_metadata.json',) 
    meta = json.load(f) 
    f.close() 
    img = []
    label = []
    for i in range(20000):
        print("doing image"+str(i))
        img.append(np.array(k.preprocessing.image.load_img('/content/drive/My Drive/InfraredSolarModules/' + meta[str(i)]['image_filepath'], target_size=(224, 224))))
        if meta[str(i)]['anomaly_class'] == 'No-Anomaly':
          label.append('No-Anomaly')
        else:
          label.append('Anomaly')  
    return np.array(img), np.array(label)
 
imgs, labels = imagegen()
print(imgs.shape)
print(len(imgs))
print(len(imgs.shape))


# + id="YPD0nHYmS9yv"

def imageLoader(imgs, batch_size):

    L = len(imgs)

    # this line is just to make the generator infinite, keras needs that    
    while True:

        batch_start = 0
        batch_end = batch_size

        while batch_start < L:
            print(batch_start)
            limit = min(batch_end, L)
            X = imgs[batch_start:limit]
            
            # Y = labels[batch_start:limit]
        # break
            yield X 
            
            batch_start += batch_size   
            batch_end += batch_size


# + id="au1bXOVO-D_4" outputId="32af1358-9bf3-4919-e7fb-68843762f5be" colab={"base_uri": "https://localhost:8080/", "height": 1000}
def preprocess_image(imgs):
  preprocessed_images = []
  counter = 1
  for image_item in imgs:
    print("doing image {}".format(counter))
    # image_item = image.img_to_array(image_item)
    image_item = np.expand_dims(image_item, axis=0)
    image_item = preprocess_input(image_item) 
    counter+=1

preprocess_image(imgs)



# + id="KG2KvH56UcIB"
model = VGG16(weights="imagenet", include_top=False, classes=2)

# + id="2f5EWYv3TII0" outputId="72e40c06-2175-41db-ee44-2122b21813d4" colab={"base_uri": "https://localhost:8080/", "height": 1000}
# print("1", labels[0])
# print("2", imgs[0].shape)
# features = model.predict_generator(imageLoader(imgs, labels, 32))
# print(model.predict(imgs[5]).shape)
# print(model.predict(imgs[-5]).shape)
# print("I PASS")

features = model.predict(imageLoader(imgs,32),steps=625)

# + id="dsYpUGo703wM" outputId="ff3e2e37-44cd-44c5-d417-e4340038285d" colab={"base_uri": "https://localhost:8080/", "height": 34}
print(features.shape)

# + id="PSF0prVENbCy" outputId="023874c1-f171-4f68-e2d1-c540530e468e" colab={"base_uri": "https://localhost:8080/", "height": 34}
type(imgs)

# + id="b4L8_k6FcJ8o" outputId="be192cea-a9a6-4f25-998e-ce0b75776a75" colab={"base_uri": "https://localhost:8080/", "height": 34}
imgs.shape

# + id="JMTVzTjEZ79e"
from keras.applications.vgg16 import preprocess_input
from keras.applications.vgg16 import VGG16 
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image

model = VGG16(weights="imagenet", include_top=False, classes=2)

# def preprocess_image(imgs):
#   preprocessed_images = []
#   counter = 1
#   for image_item in imgs:
#     print("doing image {}".format(counter))
#     # image_item = image.img_to_array(image_item)
#     image_item = np.expand_dims(image_item, axis=0)
#     image_item = preprocess_input(image_item) 
#     counter+=1

# preprocess_image(imgs)


# features = model.predict(imgs)
# print(features.shape)



# + id="BJYe9UCHvyKv"
from sklearn import svm
from sklearn.model_selection import train_test_split
from sklearn.linear_model import SGDClassifier
features = features.reshape(20000,7*7*512)
svm_classifier = SGDClassifier()

X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.35)



# + id="d_wq7lXwsYSc" outputId="f5fc626d-ec51-4dcd-fb4e-915543cbf4d8" colab={"base_uri": "https://localhost:8080/", "height": 272}
print(X_train.shape)

print(X_train.shape)
print(y_train)

# + id="DoIiQEamsWOo" outputId="e1802710-2d47-4534-a8c3-9ca19b0dd151" colab={"base_uri": "https://localhost:8080/", "height": 51}

svm_classifier.fit(X_train, y_train)

y_pred = svm_classifier.predict(X_test)

from sklearn.metrics import confusion_matrix

confusion_matrix(y_test, y_pred)

StarcoderdataJupyterScriptsDedupFiltered