widzenie-komputerowe-projekt/training/VGG16.ipynb
2023-02-01 18:42:47 +01:00

3.5 KiB

import keras, os, time
from training.data_load import load_data
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
from keras.applications import VGG16
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

train_ds, test_ds, validation_ds = load_data((224, 224), './new_data_transformed/train', './new_data_transformed/test')
def tensorboard_callback(model_name):
    return keras.callbacks.TensorBoard(os.path.join(f"./logs/{model_name}", time.strftime("run_%Y_%m_%d-%H_%M_%S")))

ssl._create_default_https_context = ssl._create_unverified_context

IMAGE_SIZE = [224, 224]

vgg2 = VGG16(input_shape=tuple(IMAGE_SIZE + [3]), include_top=False, weights='imagenet')

for layer in vgg2.layers:
  layer.trainable = False
  

x = Flatten()(vgg2.output)
prediction = Dense(6, activation='softmax')(x)

model = Model(inputs=vgg2.input, outputs=prediction)

model.summary()
model.compile(
  loss='sparse_categorical_crossentropy',
  optimizer='adam',
  metrics=['accuracy']
)

vggr = model.fit(
  train_ds,
  validation_data=validation_ds,
  epochs=15,
  steps_per_epoch=len(train_ds),
  validation_steps=len(validation_ds),
  callbacks=[tensorboard_callback("VGG16")]
)
!tensorboard dev upload --logdir logs/VGG16 --name VGGFishBest
model.evaluate(test_ds)
from training.model_freeze import freeze_model

freeze_model(model, './frozen_models', "frozen_vgg16")