164 KiB
164 KiB
Aleksandra Jonas, Aleksandra Gronowska, Iwona Christop
Zadanie 9-10, zadanie 1 - VGG16 + ResNet on train_test_sw
Przygotowanie danych
from IPython.display import Image, display
import sys
import subprocess
import pkg_resources
import numpy as np
required = { 'scikit-image'}
installed = {pkg.key for pkg in pkg_resources.working_set}
missing = required - installed
# VGG16 requires images to be of dim = (224, 224, 3)
newSize = (224,224)
if missing:
python = sys.executable
subprocess.check_call([python, '-m', 'pip', 'install', *missing], stdout=subprocess.DEVNULL)
def load_train_data(input_dir):
import numpy as np
import pandas as pd
import os
from skimage.io import imread
import cv2 as cv
from pathlib import Path
import random
from shutil import copyfile, rmtree
import json
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
image_dir = Path(input_dir)
categories_name = []
for file in os.listdir(image_dir):
d = os.path.join(image_dir, file)
if os.path.isdir(d):
categories_name.append(file)
folders = [directory for directory in image_dir.iterdir() if directory.is_dir()]
train_img = []
categories_count=[]
labels=[]
for i, direc in enumerate(folders):
count = 0
for obj in direc.iterdir():
if os.path.isfile(obj) and os.path.basename(os.path.normpath(obj)) != 'desktop.ini':
labels.append(os.path.basename(os.path.normpath(direc)))
count += 1
img = imread(obj)#zwraca ndarry postaci xSize x ySize x colorDepth
img = img[:, :, :3]
img = cv.resize(img, newSize, interpolation=cv.INTER_AREA)# zwraca ndarray
img = img / 255 #normalizacja
train_img.append(img)
categories_count.append(count)
X={}
X["values"] = np.array(train_img)
X["categories_name"] = categories_name
X["categories_count"] = categories_count
X["labels"]=labels
return X
def load_test_data(input_dir):
import numpy as np
import pandas as pd
import os
from skimage.io import imread
import cv2 as cv
from pathlib import Path
import random
from shutil import copyfile, rmtree
import json
import seaborn as sns
import matplotlib.pyplot as plt
import matplotlib
image_path = Path(input_dir)
labels_path = image_path.parents[0] / 'test_labels.json'
jsonString = labels_path.read_text()
objects = json.loads(jsonString)
categories_name = []
categories_count=[]
count = 0
c = objects[0]['value']
for e in objects:
if e['value'] != c:
categories_count.append(count)
c = e['value']
count = 1
else:
count += 1
if not e['value'] in categories_name:
categories_name.append(e['value'])
categories_count.append(count)
test_img = []
labels=[]
for e in objects:
p = image_path / e['filename']
img = imread(p)#zwraca ndarry postaci xSize x ySize x colorDepth
img = img[:, :, :3]
img = cv.resize(img, newSize, interpolation=cv.INTER_AREA)# zwraca ndarray
img = img / 255#normalizacja
test_img.append(img)
labels.append(e['value'])
X={}
X["values"] = np.array(test_img)
X["categories_name"] = categories_name
X["categories_count"] = categories_count
X["labels"]=labels
return X
def create_tf_ds(X_train, y_train_enc, X_validate, y_validate_enc, X_test, y_test_enc):
import tensorflow as tf
train_ds = tf.data.Dataset.from_tensor_slices((X_train, y_train_enc))
validation_ds = tf.data.Dataset.from_tensor_slices((X_validate, y_validate_enc))
test_ds = tf.data.Dataset.from_tensor_slices((X_test, y_test_enc))
train_ds_size = tf.data.experimental.cardinality(train_ds).numpy()
test_ds_size = tf.data.experimental.cardinality(test_ds).numpy()
validation_ds_size = tf.data.experimental.cardinality(validation_ds).numpy()
print("Training data size:", train_ds_size)
print("Test data size:", test_ds_size)
print("Validation data size:", validation_ds_size)
train_ds = (train_ds
.shuffle(buffer_size=train_ds_size)
.batch(batch_size=32, drop_remainder=True))
test_ds = (test_ds
.shuffle(buffer_size=train_ds_size)
.batch(batch_size=32, drop_remainder=True))
validation_ds = (validation_ds
.shuffle(buffer_size=train_ds_size)
.batch(batch_size=32, drop_remainder=True))
return train_ds, test_ds, validation_ds
def get_run_logdir(root_logdir):
import os
import time
run_id = time.strftime("run_%Y_%m_%d-%H_%M_%S")
return os.path.join(root_logdir, run_id)
def diagram_setup(model_name):
from tensorflow import keras
import os
root_logdir = os.path.join(os.curdir, f"logs\\\\fit\\\\\{model_name}\\\\")
run_logdir = get_run_logdir(root_logdir)
tensorboard_cb = keras.callbacks.TensorBoard(run_logdir)
# Data load
data_train = load_train_data("./train_test_sw/train_sw")
values_train = data_train['values']
labels_train = data_train['labels']
data_test = load_test_data("./train_test_sw/test_sw")
X_test = data_test['values']
y_test = data_test['labels']
from sklearn.model_selection import train_test_split
X_train, X_validate, y_train, y_validate = train_test_split(values_train, labels_train, test_size=0.2, random_state=42)
from sklearn.preprocessing import LabelEncoder
class_le = LabelEncoder()
y_train_enc = class_le.fit_transform(y_train)
y_validate_enc = class_le.fit_transform(y_validate)
y_test_enc = class_le.fit_transform(y_test)
train_ds, test_ds, validation_ds = create_tf_ds(X_train, y_train_enc, X_validate, y_validate_enc, X_test, y_test_enc)
Training data size: 821 Test data size: 259 Validation data size: 206
VGG16
diagram_setup('vgg_sw')
import keras,os
from keras.models import Sequential
from keras.layers import Dense, Conv2D, MaxPool2D , Flatten
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
model = keras.models.Sequential([
keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', input_shape=(224,224,3), padding="same"),
keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', input_shape=(224,224,3), padding="same"),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=128, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=256, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.MaxPool2D(pool_size=(2,2), strides=(2,2)),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Conv2D(filters=512, kernel_size=(3,3), padding="same", activation="relu"),
keras.layers.Flatten(),
keras.layers.Dense(units = 4096, activation='relu'),
keras.layers.Dense(units = 4096, activation='relu'),
keras.layers.Dense(units = 5, activation='softmax')
])
from keras.optimizers import Adam
opt = Adam(lr=0.001)
model.compile(optimizer=opt, loss=keras.losses.sparse_categorical_crossentropy, metrics=['accuracy'])
model.summary()
Model: "sequential_1" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_13 (Conv2D) (None, 224, 224, 64) 1792 conv2d_14 (Conv2D) (None, 224, 224, 64) 36928 max_pooling2d_4 (MaxPooling (None, 112, 112, 64) 0 2D) conv2d_15 (Conv2D) (None, 112, 112, 128) 73856 conv2d_16 (Conv2D) (None, 112, 112, 128) 147584 max_pooling2d_5 (MaxPooling (None, 56, 56, 128) 0 2D) conv2d_17 (Conv2D) (None, 56, 56, 256) 295168 conv2d_18 (Conv2D) (None, 56, 56, 256) 590080 conv2d_19 (Conv2D) (None, 56, 56, 256) 590080 max_pooling2d_6 (MaxPooling (None, 28, 28, 256) 0 2D) conv2d_20 (Conv2D) (None, 28, 28, 512) 1180160 conv2d_21 (Conv2D) (None, 28, 28, 512) 2359808 conv2d_22 (Conv2D) (None, 28, 28, 512) 2359808 max_pooling2d_7 (MaxPooling (None, 14, 14, 512) 0 2D) conv2d_23 (Conv2D) (None, 14, 14, 512) 2359808 conv2d_24 (Conv2D) (None, 14, 14, 512) 2359808 conv2d_25 (Conv2D) (None, 14, 14, 512) 2359808 flatten_2 (Flatten) (None, 100352) 0 dense_4 (Dense) (None, 4096) 411045888 dense_5 (Dense) (None, 4096) 16781312 dense_6 (Dense) (None, 5) 20485 ================================================================= Total params: 442,562,373 Trainable params: 442,562,373 Non-trainable params: 0 _________________________________________________________________
from keras.callbacks import ModelCheckpoint, EarlyStopping
checkpoint = ModelCheckpoint("vgg16_1.h5", monitor='val_accuracy', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1)
early = EarlyStopping(monitor='val_accuracy', min_delta=0, patience=20, verbose=1, mode='auto')
hist_vgg = model.fit_generator(steps_per_epoch=len(train_ds), generator=train_ds, validation_data= validation_ds, validation_steps=len(validation_ds), epochs=25, callbacks=[checkpoint,early])
WARNING:tensorflow:`period` argument is deprecated. Please use `save_freq` to specify the frequency in number of batches seen. Epoch 1/25
/var/folders/3r/c8tg1h051m18qhsdccdysrt40000gn/T/ipykernel_2029/3158629982.py:4: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators. hist_vgg = model.fit_generator(steps_per_epoch=len(train_ds), generator=train_ds, validation_data= validation_ds, validation_steps=len(validation_ds), epochs=25, callbacks=[checkpoint,early])
25/25 [==============================] - ETA: 0s - loss: 1.6264 - accuracy: 0.1900 Epoch 1: val_accuracy improved from -inf to 0.18229, saving model to vgg16_1.h5 25/25 [==============================] - 854s 34s/step - loss: 1.6264 - accuracy: 0.1900 - val_loss: 1.6109 - val_accuracy: 0.1823 Epoch 2/25 25/25 [==============================] - ETA: 0s - loss: 1.6108 - accuracy: 0.1950 Epoch 2: val_accuracy improved from 0.18229 to 0.18750, saving model to vgg16_1.h5 25/25 [==============================] - 897s 36s/step - loss: 1.6108 - accuracy: 0.1950 - val_loss: 1.6098 - val_accuracy: 0.1875 Epoch 3/25 25/25 [==============================] - ETA: 0s - loss: 1.6097 - accuracy: 0.2062 Epoch 3: val_accuracy improved from 0.18750 to 0.19792, saving model to vgg16_1.h5 25/25 [==============================] - 870s 35s/step - loss: 1.6097 - accuracy: 0.2062 - val_loss: 1.6102 - val_accuracy: 0.1979 Epoch 4/25 25/25 [==============================] - ETA: 0s - loss: 1.6097 - accuracy: 0.2037 Epoch 4: val_accuracy did not improve from 0.19792 25/25 [==============================] - 692s 28s/step - loss: 1.6097 - accuracy: 0.2037 - val_loss: 1.6106 - val_accuracy: 0.1979 Epoch 5/25 25/25 [==============================] - ETA: 0s - loss: 1.6095 - accuracy: 0.1963 Epoch 5: val_accuracy did not improve from 0.19792 25/25 [==============================] - 634s 26s/step - loss: 1.6095 - accuracy: 0.1963 - val_loss: 1.6114 - val_accuracy: 0.1823 Epoch 6/25 25/25 [==============================] - ETA: 0s - loss: 1.6094 - accuracy: 0.1925 Epoch 6: val_accuracy did not improve from 0.19792 25/25 [==============================] - 643s 26s/step - loss: 1.6094 - accuracy: 0.1925 - val_loss: 1.6112 - val_accuracy: 0.1719 Epoch 7/25 25/25 [==============================] - ETA: 0s - loss: 1.6095 - accuracy: 0.2025 Epoch 7: val_accuracy did not improve from 0.19792 25/25 [==============================] - 697s 28s/step - loss: 1.6095 - accuracy: 0.2025 - val_loss: 1.6115 - val_accuracy: 0.1823 Epoch 8/25 25/25 [==============================] - ETA: 0s - loss: 1.6097 - accuracy: 0.1762 Epoch 8: val_accuracy did not improve from 0.19792 25/25 [==============================] - 667s 27s/step - loss: 1.6097 - accuracy: 0.1762 - val_loss: 1.6106 - val_accuracy: 0.1979 Epoch 9/25 25/25 [==============================] - ETA: 0s - loss: 1.6096 - accuracy: 0.2025 Epoch 9: val_accuracy did not improve from 0.19792 25/25 [==============================] - 656s 26s/step - loss: 1.6096 - accuracy: 0.2025 - val_loss: 1.6103 - val_accuracy: 0.1927 Epoch 10/25 25/25 [==============================] - ETA: 0s - loss: 1.6095 - accuracy: 0.1950 Epoch 10: val_accuracy did not improve from 0.19792 25/25 [==============================] - 651s 26s/step - loss: 1.6095 - accuracy: 0.1950 - val_loss: 1.6104 - val_accuracy: 0.1927 Epoch 11/25 25/25 [==============================] - ETA: 0s - loss: 1.6094 - accuracy: 0.2062 Epoch 11: val_accuracy did not improve from 0.19792 25/25 [==============================] - 646s 26s/step - loss: 1.6094 - accuracy: 0.2062 - val_loss: 1.6105 - val_accuracy: 0.1927 Epoch 12/25 25/25 [==============================] - ETA: 0s - loss: 1.6095 - accuracy: 0.2062 Epoch 12: val_accuracy did not improve from 0.19792 25/25 [==============================] - 648s 26s/step - loss: 1.6095 - accuracy: 0.2062 - val_loss: 1.6103 - val_accuracy: 0.1927 Epoch 13/25 25/25 [==============================] - ETA: 0s - loss: 1.6096 - accuracy: 0.2025 Epoch 13: val_accuracy did not improve from 0.19792 25/25 [==============================] - 636s 26s/step - loss: 1.6096 - accuracy: 0.2025 - val_loss: 1.6108 - val_accuracy: 0.1927 Epoch 14/25 25/25 [==============================] - ETA: 0s - loss: 1.6097 - accuracy: 0.2050 Epoch 14: val_accuracy did not improve from 0.19792 25/25 [==============================] - 664s 27s/step - loss: 1.6097 - accuracy: 0.2050 - val_loss: 1.6110 - val_accuracy: 0.1875 Epoch 15/25 25/25 [==============================] - ETA: 0s - loss: 1.6096 - accuracy: 0.1775 Epoch 15: val_accuracy did not improve from 0.19792 25/25 [==============================] - 657s 27s/step - loss: 1.6096 - accuracy: 0.1775 - val_loss: 1.6105 - val_accuracy: 0.1875 Epoch 16/25 25/25 [==============================] - ETA: 0s - loss: 1.6095 - accuracy: 0.2000 Epoch 16: val_accuracy did not improve from 0.19792 25/25 [==============================] - 664s 27s/step - loss: 1.6095 - accuracy: 0.2000 - val_loss: 1.6102 - val_accuracy: 0.1927 Epoch 17/25 25/25 [==============================] - ETA: 0s - loss: 1.6094 - accuracy: 0.1937 Epoch 17: val_accuracy did not improve from 0.19792 25/25 [==============================] - 676s 27s/step - loss: 1.6094 - accuracy: 0.1937 - val_loss: 1.6104 - val_accuracy: 0.1927 Epoch 18/25 25/25 [==============================] - ETA: 0s - loss: 1.6093 - accuracy: 0.1975 Epoch 18: val_accuracy did not improve from 0.19792 25/25 [==============================] - 673s 27s/step - loss: 1.6093 - accuracy: 0.1975 - val_loss: 1.6103 - val_accuracy: 0.1823 Epoch 19/25 25/25 [==============================] - ETA: 0s - loss: 1.6094 - accuracy: 0.2050 Epoch 19: val_accuracy did not improve from 0.19792 25/25 [==============================] - 681s 27s/step - loss: 1.6094 - accuracy: 0.2050 - val_loss: 1.6111 - val_accuracy: 0.1771 Epoch 20/25 25/25 [==============================] - ETA: 0s - loss: 1.6093 - accuracy: 0.2050 Epoch 20: val_accuracy did not improve from 0.19792 25/25 [==============================] - 672s 27s/step - loss: 1.6093 - accuracy: 0.2050 - val_loss: 1.6108 - val_accuracy: 0.1927 Epoch 21/25 25/25 [==============================] - ETA: 0s - loss: 1.6094 - accuracy: 0.2050 Epoch 21: val_accuracy did not improve from 0.19792 25/25 [==============================] - 663s 27s/step - loss: 1.6094 - accuracy: 0.2050 - val_loss: 1.6110 - val_accuracy: 0.1927 Epoch 22/25 25/25 [==============================] - ETA: 0s - loss: 1.6096 - accuracy: 0.1850 Epoch 22: val_accuracy did not improve from 0.19792 25/25 [==============================] - 675s 27s/step - loss: 1.6096 - accuracy: 0.1850 - val_loss: 1.6111 - val_accuracy: 0.1927 Epoch 23/25 25/25 [==============================] - ETA: 0s - loss: 1.6092 - accuracy: 0.1963 Epoch 23: val_accuracy did not improve from 0.19792 25/25 [==============================] - 664s 27s/step - loss: 1.6092 - accuracy: 0.1963 - val_loss: 1.6110 - val_accuracy: 0.1823 Epoch 23: early stopping
import matplotlib.pyplot as plt
plt.plot(hist_vgg.history["accuracy"])
plt.plot(hist_vgg.history['val_accuracy'])
plt.plot(hist_vgg.history['loss'])
plt.plot(hist_vgg.history['val_loss'])
plt.title("Model accuracy")
plt.ylabel("Value")
plt.xlabel("Epoch")
plt.legend(["Accuracy","Validation Accuracy","Loss","Validation Loss"])
plt.show()
model.evaluate(test_ds)
8/8 [==============================] - 35s 4s/step - loss: 1.6097 - accuracy: 0.1953
[1.6096564531326294, 0.1953125]
ResNet50
from keras.layers import Input, Lambda, Dense, Flatten
from keras.models import Model
from keras.preprocessing import image
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Sequential
import numpy as np
from glob import glob
import matplotlib.pyplot as plt
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
from keras.applications import ResNet50
# re-size all the images to this
IMAGE_SIZE = [224, 224]
# add preprocessing layer to the front of resnet
resnet = ResNet50(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
# don't train existing weights
for layer in resnet.layers:
layer.trainable = False
# useful for getting number of classes
classes = 5
# our layers - you can add more if you want
x = Flatten()(resnet.output)
# x = Dense(1000, activation='relu')(x)
prediction = Dense(5, activation='softmax')(x)
# create a model object
model = Model(inputs=resnet.input, outputs=prediction)
# view the structure of the model
model.summary()
Model: "model_1" __________________________________________________________________________________________________ Layer (type) Output Shape Param # Connected to ================================================================================================== input_3 (InputLayer) [(None, 224, 224, 3 0 [] )] conv1_pad (ZeroPadding2D) (None, 230, 230, 3) 0 ['input_3[0][0]'] conv1_conv (Conv2D) (None, 112, 112, 64 9472 ['conv1_pad[0][0]'] ) conv1_bn (BatchNormalization) (None, 112, 112, 64 256 ['conv1_conv[0][0]'] ) conv1_relu (Activation) (None, 112, 112, 64 0 ['conv1_bn[0][0]'] ) pool1_pad (ZeroPadding2D) (None, 114, 114, 64 0 ['conv1_relu[0][0]'] ) pool1_pool (MaxPooling2D) (None, 56, 56, 64) 0 ['pool1_pad[0][0]'] conv2_block1_1_conv (Conv2D) (None, 56, 56, 64) 4160 ['pool1_pool[0][0]'] conv2_block1_1_bn (BatchNormal (None, 56, 56, 64) 256 ['conv2_block1_1_conv[0][0]'] ization) conv2_block1_1_relu (Activatio (None, 56, 56, 64) 0 ['conv2_block1_1_bn[0][0]'] n) conv2_block1_2_conv (Conv2D) (None, 56, 56, 64) 36928 ['conv2_block1_1_relu[0][0]'] conv2_block1_2_bn (BatchNormal (None, 56, 56, 64) 256 ['conv2_block1_2_conv[0][0]'] ization) conv2_block1_2_relu (Activatio (None, 56, 56, 64) 0 ['conv2_block1_2_bn[0][0]'] n) conv2_block1_0_conv (Conv2D) (None, 56, 56, 256) 16640 ['pool1_pool[0][0]'] conv2_block1_3_conv (Conv2D) (None, 56, 56, 256) 16640 ['conv2_block1_2_relu[0][0]'] conv2_block1_0_bn (BatchNormal (None, 56, 56, 256) 1024 ['conv2_block1_0_conv[0][0]'] ization) conv2_block1_3_bn (BatchNormal (None, 56, 56, 256) 1024 ['conv2_block1_3_conv[0][0]'] ization) conv2_block1_add (Add) (None, 56, 56, 256) 0 ['conv2_block1_0_bn[0][0]', 'conv2_block1_3_bn[0][0]'] conv2_block1_out (Activation) (None, 56, 56, 256) 0 ['conv2_block1_add[0][0]'] conv2_block2_1_conv (Conv2D) (None, 56, 56, 64) 16448 ['conv2_block1_out[0][0]'] conv2_block2_1_bn (BatchNormal (None, 56, 56, 64) 256 ['conv2_block2_1_conv[0][0]'] ization) conv2_block2_1_relu (Activatio (None, 56, 56, 64) 0 ['conv2_block2_1_bn[0][0]'] n) conv2_block2_2_conv (Conv2D) (None, 56, 56, 64) 36928 ['conv2_block2_1_relu[0][0]'] conv2_block2_2_bn (BatchNormal (None, 56, 56, 64) 256 ['conv2_block2_2_conv[0][0]'] ization) conv2_block2_2_relu (Activatio (None, 56, 56, 64) 0 ['conv2_block2_2_bn[0][0]'] n) conv2_block2_3_conv (Conv2D) (None, 56, 56, 256) 16640 ['conv2_block2_2_relu[0][0]'] conv2_block2_3_bn (BatchNormal (None, 56, 56, 256) 1024 ['conv2_block2_3_conv[0][0]'] ization) conv2_block2_add (Add) (None, 56, 56, 256) 0 ['conv2_block1_out[0][0]', 'conv2_block2_3_bn[0][0]'] conv2_block2_out (Activation) (None, 56, 56, 256) 0 ['conv2_block2_add[0][0]'] conv2_block3_1_conv (Conv2D) (None, 56, 56, 64) 16448 ['conv2_block2_out[0][0]'] conv2_block3_1_bn (BatchNormal (None, 56, 56, 64) 256 ['conv2_block3_1_conv[0][0]'] ization) conv2_block3_1_relu (Activatio (None, 56, 56, 64) 0 ['conv2_block3_1_bn[0][0]'] n) conv2_block3_2_conv (Conv2D) (None, 56, 56, 64) 36928 ['conv2_block3_1_relu[0][0]'] conv2_block3_2_bn (BatchNormal (None, 56, 56, 64) 256 ['conv2_block3_2_conv[0][0]'] ization) conv2_block3_2_relu (Activatio (None, 56, 56, 64) 0 ['conv2_block3_2_bn[0][0]'] n) conv2_block3_3_conv (Conv2D) (None, 56, 56, 256) 16640 ['conv2_block3_2_relu[0][0]'] conv2_block3_3_bn (BatchNormal (None, 56, 56, 256) 1024 ['conv2_block3_3_conv[0][0]'] ization) conv2_block3_add (Add) (None, 56, 56, 256) 0 ['conv2_block2_out[0][0]', 'conv2_block3_3_bn[0][0]'] conv2_block3_out (Activation) (None, 56, 56, 256) 0 ['conv2_block3_add[0][0]'] conv3_block1_1_conv (Conv2D) (None, 28, 28, 128) 32896 ['conv2_block3_out[0][0]'] conv3_block1_1_bn (BatchNormal (None, 28, 28, 128) 512 ['conv3_block1_1_conv[0][0]'] ization) conv3_block1_1_relu (Activatio (None, 28, 28, 128) 0 ['conv3_block1_1_bn[0][0]'] n) conv3_block1_2_conv (Conv2D) (None, 28, 28, 128) 147584 ['conv3_block1_1_relu[0][0]'] conv3_block1_2_bn (BatchNormal (None, 28, 28, 128) 512 ['conv3_block1_2_conv[0][0]'] ization) conv3_block1_2_relu (Activatio (None, 28, 28, 128) 0 ['conv3_block1_2_bn[0][0]'] n) conv3_block1_0_conv (Conv2D) (None, 28, 28, 512) 131584 ['conv2_block3_out[0][0]'] conv3_block1_3_conv (Conv2D) (None, 28, 28, 512) 66048 ['conv3_block1_2_relu[0][0]'] conv3_block1_0_bn (BatchNormal (None, 28, 28, 512) 2048 ['conv3_block1_0_conv[0][0]'] ization) conv3_block1_3_bn (BatchNormal (None, 28, 28, 512) 2048 ['conv3_block1_3_conv[0][0]'] ization) conv3_block1_add (Add) (None, 28, 28, 512) 0 ['conv3_block1_0_bn[0][0]', 'conv3_block1_3_bn[0][0]'] conv3_block1_out (Activation) (None, 28, 28, 512) 0 ['conv3_block1_add[0][0]'] conv3_block2_1_conv (Conv2D) (None, 28, 28, 128) 65664 ['conv3_block1_out[0][0]'] conv3_block2_1_bn (BatchNormal (None, 28, 28, 128) 512 ['conv3_block2_1_conv[0][0]'] ization) conv3_block2_1_relu (Activatio (None, 28, 28, 128) 0 ['conv3_block2_1_bn[0][0]'] n) conv3_block2_2_conv (Conv2D) (None, 28, 28, 128) 147584 ['conv3_block2_1_relu[0][0]'] conv3_block2_2_bn (BatchNormal (None, 28, 28, 128) 512 ['conv3_block2_2_conv[0][0]'] ization) conv3_block2_2_relu (Activatio (None, 28, 28, 128) 0 ['conv3_block2_2_bn[0][0]'] n) conv3_block2_3_conv (Conv2D) (None, 28, 28, 512) 66048 ['conv3_block2_2_relu[0][0]'] conv3_block2_3_bn (BatchNormal (None, 28, 28, 512) 2048 ['conv3_block2_3_conv[0][0]'] ization) conv3_block2_add (Add) (None, 28, 28, 512) 0 ['conv3_block1_out[0][0]', 'conv3_block2_3_bn[0][0]'] conv3_block2_out (Activation) (None, 28, 28, 512) 0 ['conv3_block2_add[0][0]'] conv3_block3_1_conv (Conv2D) (None, 28, 28, 128) 65664 ['conv3_block2_out[0][0]'] conv3_block3_1_bn (BatchNormal (None, 28, 28, 128) 512 ['conv3_block3_1_conv[0][0]'] ization) conv3_block3_1_relu (Activatio (None, 28, 28, 128) 0 ['conv3_block3_1_bn[0][0]'] n) conv3_block3_2_conv (Conv2D) (None, 28, 28, 128) 147584 ['conv3_block3_1_relu[0][0]'] conv3_block3_2_bn (BatchNormal (None, 28, 28, 128) 512 ['conv3_block3_2_conv[0][0]'] ization) conv3_block3_2_relu (Activatio (None, 28, 28, 128) 0 ['conv3_block3_2_bn[0][0]'] n) conv3_block3_3_conv (Conv2D) (None, 28, 28, 512) 66048 ['conv3_block3_2_relu[0][0]'] conv3_block3_3_bn (BatchNormal (None, 28, 28, 512) 2048 ['conv3_block3_3_conv[0][0]'] ization) conv3_block3_add (Add) (None, 28, 28, 512) 0 ['conv3_block2_out[0][0]', 'conv3_block3_3_bn[0][0]'] conv3_block3_out (Activation) (None, 28, 28, 512) 0 ['conv3_block3_add[0][0]'] conv3_block4_1_conv (Conv2D) (None, 28, 28, 128) 65664 ['conv3_block3_out[0][0]'] conv3_block4_1_bn (BatchNormal (None, 28, 28, 128) 512 ['conv3_block4_1_conv[0][0]'] ization) conv3_block4_1_relu (Activatio (None, 28, 28, 128) 0 ['conv3_block4_1_bn[0][0]'] n) conv3_block4_2_conv (Conv2D) (None, 28, 28, 128) 147584 ['conv3_block4_1_relu[0][0]'] conv3_block4_2_bn (BatchNormal (None, 28, 28, 128) 512 ['conv3_block4_2_conv[0][0]'] ization) conv3_block4_2_relu (Activatio (None, 28, 28, 128) 0 ['conv3_block4_2_bn[0][0]'] n) conv3_block4_3_conv (Conv2D) (None, 28, 28, 512) 66048 ['conv3_block4_2_relu[0][0]'] conv3_block4_3_bn (BatchNormal (None, 28, 28, 512) 2048 ['conv3_block4_3_conv[0][0]'] ization) conv3_block4_add (Add) (None, 28, 28, 512) 0 ['conv3_block3_out[0][0]', 'conv3_block4_3_bn[0][0]'] conv3_block4_out (Activation) (None, 28, 28, 512) 0 ['conv3_block4_add[0][0]'] conv4_block1_1_conv (Conv2D) (None, 14, 14, 256) 131328 ['conv3_block4_out[0][0]'] conv4_block1_1_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block1_1_conv[0][0]'] ization) conv4_block1_1_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block1_1_bn[0][0]'] n) conv4_block1_2_conv (Conv2D) (None, 14, 14, 256) 590080 ['conv4_block1_1_relu[0][0]'] conv4_block1_2_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block1_2_conv[0][0]'] ization) conv4_block1_2_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block1_2_bn[0][0]'] n) conv4_block1_0_conv (Conv2D) (None, 14, 14, 1024 525312 ['conv3_block4_out[0][0]'] ) conv4_block1_3_conv (Conv2D) (None, 14, 14, 1024 263168 ['conv4_block1_2_relu[0][0]'] ) conv4_block1_0_bn (BatchNormal (None, 14, 14, 1024 4096 ['conv4_block1_0_conv[0][0]'] ization) ) conv4_block1_3_bn (BatchNormal (None, 14, 14, 1024 4096 ['conv4_block1_3_conv[0][0]'] ization) ) conv4_block1_add (Add) (None, 14, 14, 1024 0 ['conv4_block1_0_bn[0][0]', ) 'conv4_block1_3_bn[0][0]'] conv4_block1_out (Activation) (None, 14, 14, 1024 0 ['conv4_block1_add[0][0]'] ) conv4_block2_1_conv (Conv2D) (None, 14, 14, 256) 262400 ['conv4_block1_out[0][0]'] conv4_block2_1_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block2_1_conv[0][0]'] ization) conv4_block2_1_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block2_1_bn[0][0]'] n) conv4_block2_2_conv (Conv2D) (None, 14, 14, 256) 590080 ['conv4_block2_1_relu[0][0]'] conv4_block2_2_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block2_2_conv[0][0]'] ization) conv4_block2_2_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block2_2_bn[0][0]'] n) conv4_block2_3_conv (Conv2D) (None, 14, 14, 1024 263168 ['conv4_block2_2_relu[0][0]'] ) conv4_block2_3_bn (BatchNormal (None, 14, 14, 1024 4096 ['conv4_block2_3_conv[0][0]'] ization) ) conv4_block2_add (Add) (None, 14, 14, 1024 0 ['conv4_block1_out[0][0]', ) 'conv4_block2_3_bn[0][0]'] conv4_block2_out (Activation) (None, 14, 14, 1024 0 ['conv4_block2_add[0][0]'] ) conv4_block3_1_conv (Conv2D) (None, 14, 14, 256) 262400 ['conv4_block2_out[0][0]'] conv4_block3_1_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block3_1_conv[0][0]'] ization) conv4_block3_1_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block3_1_bn[0][0]'] n) conv4_block3_2_conv (Conv2D) (None, 14, 14, 256) 590080 ['conv4_block3_1_relu[0][0]'] conv4_block3_2_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block3_2_conv[0][0]'] ization) conv4_block3_2_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block3_2_bn[0][0]'] n) conv4_block3_3_conv (Conv2D) (None, 14, 14, 1024 263168 ['conv4_block3_2_relu[0][0]'] ) conv4_block3_3_bn (BatchNormal (None, 14, 14, 1024 4096 ['conv4_block3_3_conv[0][0]'] ization) ) conv4_block3_add (Add) (None, 14, 14, 1024 0 ['conv4_block2_out[0][0]', ) 'conv4_block3_3_bn[0][0]'] conv4_block3_out (Activation) (None, 14, 14, 1024 0 ['conv4_block3_add[0][0]'] ) conv4_block4_1_conv (Conv2D) (None, 14, 14, 256) 262400 ['conv4_block3_out[0][0]'] conv4_block4_1_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block4_1_conv[0][0]'] ization) conv4_block4_1_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block4_1_bn[0][0]'] n) conv4_block4_2_conv (Conv2D) (None, 14, 14, 256) 590080 ['conv4_block4_1_relu[0][0]'] conv4_block4_2_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block4_2_conv[0][0]'] ization) conv4_block4_2_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block4_2_bn[0][0]'] n) conv4_block4_3_conv (Conv2D) (None, 14, 14, 1024 263168 ['conv4_block4_2_relu[0][0]'] ) conv4_block4_3_bn (BatchNormal (None, 14, 14, 1024 4096 ['conv4_block4_3_conv[0][0]'] ization) ) conv4_block4_add (Add) (None, 14, 14, 1024 0 ['conv4_block3_out[0][0]', ) 'conv4_block4_3_bn[0][0]'] conv4_block4_out (Activation) (None, 14, 14, 1024 0 ['conv4_block4_add[0][0]'] ) conv4_block5_1_conv (Conv2D) (None, 14, 14, 256) 262400 ['conv4_block4_out[0][0]'] conv4_block5_1_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block5_1_conv[0][0]'] ization) conv4_block5_1_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block5_1_bn[0][0]'] n) conv4_block5_2_conv (Conv2D) (None, 14, 14, 256) 590080 ['conv4_block5_1_relu[0][0]'] conv4_block5_2_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block5_2_conv[0][0]'] ization) conv4_block5_2_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block5_2_bn[0][0]'] n) conv4_block5_3_conv (Conv2D) (None, 14, 14, 1024 263168 ['conv4_block5_2_relu[0][0]'] ) conv4_block5_3_bn (BatchNormal (None, 14, 14, 1024 4096 ['conv4_block5_3_conv[0][0]'] ization) ) conv4_block5_add (Add) (None, 14, 14, 1024 0 ['conv4_block4_out[0][0]', ) 'conv4_block5_3_bn[0][0]'] conv4_block5_out (Activation) (None, 14, 14, 1024 0 ['conv4_block5_add[0][0]'] ) conv4_block6_1_conv (Conv2D) (None, 14, 14, 256) 262400 ['conv4_block5_out[0][0]'] conv4_block6_1_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block6_1_conv[0][0]'] ization) conv4_block6_1_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block6_1_bn[0][0]'] n) conv4_block6_2_conv (Conv2D) (None, 14, 14, 256) 590080 ['conv4_block6_1_relu[0][0]'] conv4_block6_2_bn (BatchNormal (None, 14, 14, 256) 1024 ['conv4_block6_2_conv[0][0]'] ization) conv4_block6_2_relu (Activatio (None, 14, 14, 256) 0 ['conv4_block6_2_bn[0][0]'] n) conv4_block6_3_conv (Conv2D) (None, 14, 14, 1024 263168 ['conv4_block6_2_relu[0][0]'] ) conv4_block6_3_bn (BatchNormal (None, 14, 14, 1024 4096 ['conv4_block6_3_conv[0][0]'] ization) ) conv4_block6_add (Add) (None, 14, 14, 1024 0 ['conv4_block5_out[0][0]', ) 'conv4_block6_3_bn[0][0]'] conv4_block6_out (Activation) (None, 14, 14, 1024 0 ['conv4_block6_add[0][0]'] ) conv5_block1_1_conv (Conv2D) (None, 7, 7, 512) 524800 ['conv4_block6_out[0][0]'] conv5_block1_1_bn (BatchNormal (None, 7, 7, 512) 2048 ['conv5_block1_1_conv[0][0]'] ization) conv5_block1_1_relu (Activatio (None, 7, 7, 512) 0 ['conv5_block1_1_bn[0][0]'] n) conv5_block1_2_conv (Conv2D) (None, 7, 7, 512) 2359808 ['conv5_block1_1_relu[0][0]'] conv5_block1_2_bn (BatchNormal (None, 7, 7, 512) 2048 ['conv5_block1_2_conv[0][0]'] ization) conv5_block1_2_relu (Activatio (None, 7, 7, 512) 0 ['conv5_block1_2_bn[0][0]'] n) conv5_block1_0_conv (Conv2D) (None, 7, 7, 2048) 2099200 ['conv4_block6_out[0][0]'] conv5_block1_3_conv (Conv2D) (None, 7, 7, 2048) 1050624 ['conv5_block1_2_relu[0][0]'] conv5_block1_0_bn (BatchNormal (None, 7, 7, 2048) 8192 ['conv5_block1_0_conv[0][0]'] ization) conv5_block1_3_bn (BatchNormal (None, 7, 7, 2048) 8192 ['conv5_block1_3_conv[0][0]'] ization) conv5_block1_add (Add) (None, 7, 7, 2048) 0 ['conv5_block1_0_bn[0][0]', 'conv5_block1_3_bn[0][0]'] conv5_block1_out (Activation) (None, 7, 7, 2048) 0 ['conv5_block1_add[0][0]'] conv5_block2_1_conv (Conv2D) (None, 7, 7, 512) 1049088 ['conv5_block1_out[0][0]'] conv5_block2_1_bn (BatchNormal (None, 7, 7, 512) 2048 ['conv5_block2_1_conv[0][0]'] ization) conv5_block2_1_relu (Activatio (None, 7, 7, 512) 0 ['conv5_block2_1_bn[0][0]'] n) conv5_block2_2_conv (Conv2D) (None, 7, 7, 512) 2359808 ['conv5_block2_1_relu[0][0]'] conv5_block2_2_bn (BatchNormal (None, 7, 7, 512) 2048 ['conv5_block2_2_conv[0][0]'] ization) conv5_block2_2_relu (Activatio (None, 7, 7, 512) 0 ['conv5_block2_2_bn[0][0]'] n) conv5_block2_3_conv (Conv2D) (None, 7, 7, 2048) 1050624 ['conv5_block2_2_relu[0][0]'] conv5_block2_3_bn (BatchNormal (None, 7, 7, 2048) 8192 ['conv5_block2_3_conv[0][0]'] ization) conv5_block2_add (Add) (None, 7, 7, 2048) 0 ['conv5_block1_out[0][0]', 'conv5_block2_3_bn[0][0]'] conv5_block2_out (Activation) (None, 7, 7, 2048) 0 ['conv5_block2_add[0][0]'] conv5_block3_1_conv (Conv2D) (None, 7, 7, 512) 1049088 ['conv5_block2_out[0][0]'] conv5_block3_1_bn (BatchNormal (None, 7, 7, 512) 2048 ['conv5_block3_1_conv[0][0]'] ization) conv5_block3_1_relu (Activatio (None, 7, 7, 512) 0 ['conv5_block3_1_bn[0][0]'] n) conv5_block3_2_conv (Conv2D) (None, 7, 7, 512) 2359808 ['conv5_block3_1_relu[0][0]'] conv5_block3_2_bn (BatchNormal (None, 7, 7, 512) 2048 ['conv5_block3_2_conv[0][0]'] ization) conv5_block3_2_relu (Activatio (None, 7, 7, 512) 0 ['conv5_block3_2_bn[0][0]'] n) conv5_block3_3_conv (Conv2D) (None, 7, 7, 2048) 1050624 ['conv5_block3_2_relu[0][0]'] conv5_block3_3_bn (BatchNormal (None, 7, 7, 2048) 8192 ['conv5_block3_3_conv[0][0]'] ization) conv5_block3_add (Add) (None, 7, 7, 2048) 0 ['conv5_block2_out[0][0]', 'conv5_block3_3_bn[0][0]'] conv5_block3_out (Activation) (None, 7, 7, 2048) 0 ['conv5_block3_add[0][0]'] flatten_3 (Flatten) (None, 100352) 0 ['conv5_block3_out[0][0]'] dense_7 (Dense) (None, 5) 501765 ['flatten_3[0][0]'] ================================================================================================== Total params: 24,089,477 Trainable params: 501,765 Non-trainable params: 23,587,712 __________________________________________________________________________________________________
# tell the model what cost and optimization method to use
model.compile(
loss='sparse_categorical_crossentropy',
optimizer='adam',
metrics=['accuracy']
)
#train_ds_vgg_sw, test_ds_vgg_sw, validation_ds_vgg_sw
# fit the model
r = model.fit_generator(
train_ds,
validation_data=validation_ds,
epochs=25,
steps_per_epoch=len(train_ds),
validation_steps=len(validation_ds)
)
Epoch 1/25
/var/folders/3r/c8tg1h051m18qhsdccdysrt40000gn/T/ipykernel_2029/3602206220.py:10: UserWarning: `Model.fit_generator` is deprecated and will be removed in a future version. Please use `Model.fit`, which supports generators. r = model.fit_generator(
25/25 [==============================] - 38s 1s/step - loss: 6.4809 - accuracy: 0.1950 - val_loss: 2.9878 - val_accuracy: 0.2344 Epoch 2/25 25/25 [==============================] - 35s 1s/step - loss: 2.2159 - accuracy: 0.2338 - val_loss: 2.3206 - val_accuracy: 0.2396 Epoch 3/25 25/25 [==============================] - 35s 1s/step - loss: 1.9435 - accuracy: 0.2237 - val_loss: 1.8788 - val_accuracy: 0.2292 Epoch 4/25 25/25 [==============================] - 35s 1s/step - loss: 2.1113 - accuracy: 0.2350 - val_loss: 1.5820 - val_accuracy: 0.2604 Epoch 5/25 25/25 [==============================] - 36s 1s/step - loss: 1.7911 - accuracy: 0.2975 - val_loss: 1.6257 - val_accuracy: 0.3229 Epoch 6/25 25/25 [==============================] - 36s 1s/step - loss: 1.8471 - accuracy: 0.2975 - val_loss: 1.6844 - val_accuracy: 0.3542 Epoch 7/25 25/25 [==============================] - 36s 1s/step - loss: 1.7567 - accuracy: 0.3075 - val_loss: 1.4758 - val_accuracy: 0.3281 Epoch 8/25 25/25 [==============================] - 36s 1s/step - loss: 1.6541 - accuracy: 0.3550 - val_loss: 1.6412 - val_accuracy: 0.2708 Epoch 9/25 25/25 [==============================] - 36s 1s/step - loss: 1.4498 - accuracy: 0.3762 - val_loss: 1.3539 - val_accuracy: 0.3958 Epoch 10/25 25/25 [==============================] - 36s 1s/step - loss: 1.5093 - accuracy: 0.3525 - val_loss: 1.4342 - val_accuracy: 0.3385 Epoch 11/25 25/25 [==============================] - 36s 1s/step - loss: 1.4125 - accuracy: 0.4062 - val_loss: 1.6245 - val_accuracy: 0.3438 Epoch 12/25 25/25 [==============================] - 36s 1s/step - loss: 1.5308 - accuracy: 0.3650 - val_loss: 1.6150 - val_accuracy: 0.2292 Epoch 13/25 25/25 [==============================] - 36s 1s/step - loss: 1.4149 - accuracy: 0.4263 - val_loss: 1.5404 - val_accuracy: 0.3906 Epoch 14/25 25/25 [==============================] - 36s 1s/step - loss: 1.4894 - accuracy: 0.3925 - val_loss: 1.8275 - val_accuracy: 0.2292 Epoch 15/25 25/25 [==============================] - 36s 1s/step - loss: 1.5978 - accuracy: 0.3775 - val_loss: 1.3376 - val_accuracy: 0.4375 Epoch 16/25 25/25 [==============================] - 36s 1s/step - loss: 1.4227 - accuracy: 0.4175 - val_loss: 1.5674 - val_accuracy: 0.3958 Epoch 17/25 25/25 [==============================] - 36s 1s/step - loss: 1.4758 - accuracy: 0.3837 - val_loss: 1.5279 - val_accuracy: 0.3698 Epoch 18/25 25/25 [==============================] - 36s 1s/step - loss: 1.6931 - accuracy: 0.4137 - val_loss: 1.8716 - val_accuracy: 0.2865 Epoch 19/25 25/25 [==============================] - 36s 1s/step - loss: 1.3378 - accuracy: 0.4500 - val_loss: 1.4395 - val_accuracy: 0.4271 Epoch 20/25 25/25 [==============================] - 36s 1s/step - loss: 1.3173 - accuracy: 0.4825 - val_loss: 1.2535 - val_accuracy: 0.4583 Epoch 21/25 25/25 [==============================] - 36s 1s/step - loss: 1.2702 - accuracy: 0.4900 - val_loss: 1.4282 - val_accuracy: 0.4896 Epoch 22/25 25/25 [==============================] - 36s 1s/step - loss: 1.2848 - accuracy: 0.4600 - val_loss: 1.3511 - val_accuracy: 0.4115 Epoch 23/25 25/25 [==============================] - 36s 1s/step - loss: 1.4002 - accuracy: 0.4487 - val_loss: 1.5821 - val_accuracy: 0.3281 Epoch 24/25 25/25 [==============================] - 36s 1s/step - loss: 1.2507 - accuracy: 0.4800 - val_loss: 1.2901 - val_accuracy: 0.4635 Epoch 25/25 25/25 [==============================] - 36s 1s/step - loss: 1.3626 - accuracy: 0.4462 - val_loss: 1.5347 - val_accuracy: 0.3906
# loss
plt.plot(r.history["accuracy"])
plt.plot(r.history['val_accuracy'])
plt.plot(r.history['loss'])
plt.plot(r.history['val_loss'])
plt.title("Model accuracy")
plt.ylabel("Value")
plt.xlabel("Epoch")
plt.legend(["Accuracy","Validation Accuracy","Loss","Validation Loss"])
plt.show()
model.save('resnet_1.h5')
model.evaluate(test_ds)
8/8 [==============================] - 9s 1s/step - loss: 1.4028 - accuracy: 0.4141
[1.4027552604675293, 0.4140625]