<jupyter_start><jupyter_text><jupyter_code># -*- coding: utf-8 -*-
"""
Created on Thu May 13 03:02:04 2021

@author: BRIJB
"""

from tensorflow.keras.preprocessing.image import ImageDataGenerator
# import the needed packages
import tensorflow as tf
from keras import losses 
from keras import optimizers 
from keras import metrics 
import matplotlib.pyplot as plt
import matplotlib.image as img
import tensorflow.keras as keras
from keras.preprocessing import image
import numpy as np
from keras.models import model_from_json
import os

batch_size = 30

# define and move to dataset directory
datasetdir = "/content/drive/MyDrive/Data/Sentinel 2 Data for CNN/EuroSAT/2750"
import os
os.chdir(datasetdir)



# shortcut to the ImageDataGenerator class
ImageDataGenerator = keras.preprocessing.image.ImageDataGenerator
gen = ImageDataGenerator()
iterator = gen.flow_from_directory(
    os.getcwd(), 
    target_size=(64,64), 
    classes=('Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential','SeaLake')
    
)
# we can guess that the iterator has a next function, 
# because all python iterators have one. 
batch = iterator.next()
print(len(batch))
print(type(batch[0]))
print(batch[0].shape)
print(batch[0].dtype)
#print(batch[0].max())
#print(batch[1].shape)
#print(batch[1].dtype)
#print(type(batch[1]))
#the first element is an array of 32 images with 64X64 pixels, and 3 color channels, encoded as floats in the range 0 to 255
#The second element contains the 32 corresponding labels.


batch = iterator.next()
print(len(batch))
print(type(batch[1]))
print(batch[1].shape)
print(batch[1].dtype)
imgdatagen = ImageDataGenerator(
    rescale = 1/255., 
    validation_split = 0.2,
)

batch_size = 30
height, width = (64,64)
train_dataset = imgdatagen.flow_from_directory(
    os.getcwd(),
    target_size = (height, width), 
    classes = ('Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential','SeaLake'),
    batch_size = batch_size,
    subset = 'training'
)

val_dataset = imgdatagen.flow_from_directory(
    os.getcwd(),
    target_size = (height, width), 
    classes = ('Forest', 'HerbaceousVegetation', 'Highway', 'Industrial', 'Pasture', 'PermanentCrop', 'Residential','SeaLake'),
    batch_size = batch_size,
    subset = 'validation'
)
#In this case out of 5000 images: Training: 4000 Validation: 20% of 4000 i.e. 1000
model = keras.models.Sequential()

initializers = {
    
}

model.add( 
    keras.layers.Conv2D(
        24, 5, input_shape=(64,64,3), 
        activation='relu', 
    )
)
model.add( keras.layers.MaxPooling2D(2) )
model.add( 
    keras.layers.Conv2D(
        48, 5, activation='relu', 
    )
)
model.add( keras.layers.MaxPooling2D(2) )
model.add( 
    keras.layers.Conv2D(
        96, 5, activation='relu', 
    )
)
model.add( keras.layers.Flatten() )
model.add( keras.layers.Dropout(0.4) )

model.add( keras.layers.Dense(
    8, activation='softmax',
    )
)

model.summary()
model.compile(loss='binary_crossentropy',
              optimizer=keras.optimizers.Adamax(lr=0.001),
              metrics=['acc'])
history = model.fit_generator(
    train_dataset, 
    validation_data = val_dataset,
    workers=10,
    epochs=1,
)

#result of this model is not impressive, but can be imroved 
#443/443 [==============================] - 363s 802ms/step - loss: 0.3290 - acc: 0.3072 - val_loss: 0.2291 - val_acc: 0.5466
import glob
import numpy as np
from PIL import Image
# https://stackoverflow.com/questions/37747021/create-numpy-array-of-images
X_data = []
files = glob.glob(r"/content/drive/MyDrive/Data/data/*.jpg")
for my_file in files:
    print(my_file)
    
    #image = Image.open(my_file).convert('RGB')
    image = Image.open(my_file)
    image = np.array(image)
   
    X_data.append(image)
X_data = np.array(X_data)
x = np.array(X_data[0])
print('X_data shape:', np.array(X_data).shape)
# If you have 20 64x64 images, this would print 'X_data shape: (20, 64,64,3)' 
# @tf.function(experimental_relax_shapes=True)
# def predict(x):
#      return model.predict(x)
output = model.predict(X_data)
print(output)
# define and move to dataset directory
datasetdir = "/content/drive/MyDrive/Data/Sentinel 2 Data for CNN/EuroSAT"
import os
os.chdir(datasetdir)
model_1_EUROSAT_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_1_EUROSAT_json)
# serialize weights to HDF5
model.save_weights("model_1_EUROSAT_json.h5")
print("Saved model to disk")<jupyter_output>Saved model to disk
StarcoderdataJupyterStructuredCleanDedup