text_plain_output_1.png
from keras import layers

from keras import models

from tensorflow.keras.preprocessing.image import ImageDataGenerator

import numpy as np # linear algebra

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import tensorflow as tf

import tensorflow as tf

proj_dir = '../input/cassava-leaf-disease-classification/train_images/'
train = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')
train.loc[:, 'label'] = train.loc[:, 'label'].astype('str')
BATCH_SIZE = 64
SPLIT = 0.2

import tensorflow as tf
conv_base = tf.keras.models.load_model('../input/pretrained-models/vgg16')

conv_base.summary()

train_datagen_v2 = ImageDataGenerator(rescale=1.0 / 255, horizontal_flip=True)
test_datagen_v2 = ImageDataGenerator(rescale=1.0 / 255)
train_generator = train_datagen_v2.flow_from_dataframe(train, directory=proj_dir, x_col='image_id', y_col='label', target_size=(448, 448), batch_size=BATCH_SIZE, class_mode='categorical')
test_dir = '../input/cassava-leaf-disease-classification/test_images/'
test = pd.DataFrame()
test['image_id'] = os.listdir('../input/cassava-leaf-disease-classification/test_images/')
test_generator = test_datagen_v2.flow_from_dataframe(test, directory=test_dir, x_col='image_id', target_size=(448, 448), batch_size=1, class_mode=None, shuffle=False)

train_generator.class_indices

with tf.device('/GPU:0'):
    model = models.Sequential()
    model.add(conv_base)
    model.add(layers.MaxPooling2D((2, 2)))
    model.add(layers.Flatten())
    model.add(layers.Dense(1024, activation='relu'))
    model.add(layers.Dense(256, activation='relu'))
    model.add(layers.Dense(5, activation='softmax'))
    conv_base.trainable = True
    set_trainable = False
    for layer in conv_base.layers:
        if layer.name == 'block5_conv1':
            set_trainable = True
        if set_trainable:
            layer.trainable = True
        else:
            layer.trainable = False
    from keras import optimizers
    model.compile(loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.2), optimizer='Adamax', metrics=['acc'])
    history = model.fit_generator(train_generator, epochs=15, steps_per_epoch=len(train) / BATCH_SIZE)

test_generator.reset()
pred = model.predict_generator(test_generator, verbose=1, steps=len(test))
predicted_class_indices = np.argmax(pred, axis=1)
labels = train_generator.class_indices
labels = dict(((v, k) for k, v in labels.items()))
predictions = [labels[k] for k in predicted_class_indices]
filenames = test_generator.filenames
results = pd.DataFrame({'image_id': filenames, 'label': predictions})