text_plain_output_1.png
from tensorflow.keras import Model

from tensorflow.keras.applications.vgg16 import VGG16

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import matplotlib.pyplot as plt

import os

import tensorflow as tf

def list_files(startpath):
    for root, dirs, files in os.walk(startpath):
        level = root.replace(startpath, '').count(os.sep)
        indent = ' ' * 4 * level
        subindent = ' ' * 4 * (level + 1)

train_dir = '../input/seg_train/seg_train/'

test_dir = '../input/seg_test/seg_test/'

from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_datagen = ImageDataGenerator(rescale=1.0 / 255, rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest')
test_datagen = ImageDataGenerator(rescale=1.0 / 255.0)
train_generator = train_datagen.flow_from_directory(train_dir, batch_size=32, class_mode='categorical', target_size=(image_width, image_height))
test_generator = test_datagen.flow_from_directory(test_dir, batch_size=20, class_mode='categorical', target_size=(image_width, image_height))

from tensorflow.keras.applications.vgg16 import VGG16
vgg16 = VGG16(input_shape=(image_width, image_height, 3), include_top=False, weights='imagenet')
for layer in vgg16.layers:
    layer.trainable = False
vgg16.summary()

from tensorflow.keras import Model
first_layer = vgg16.get_layer(index=0)
last_layer = vgg16.get_layer(index=-1)
vgg16_partical = Model(inputs=first_layer.input, outputs=last_layer.output)

from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization
model_using_vgg16 = tf.keras.models.Sequential([vgg16_partical, Flatten(), Dense(512, activation='relu'), BatchNormalization(), Dropout(0.5), Dense(6, activation='softmax')])
model_using_vgg16.summary()

model_using_vgg16.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])

history = model_using_vgg16.fit_generator(train_generator, validation_data=test_generator, epochs=15)

import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(acc))
plt.plot(epochs, acc)
plt.plot(epochs, val_acc)
plt.title('Training and validation accuracy')
plt.figure()
plt.plot(epochs, loss)
plt.plot(epochs, val_loss)
plt.title('Training and validation loss')