<jupyter_start><jupyter_text>Flowers  Download the data<jupyter_code>!mkdir data
!curl http://download.tensorflow.org/example_images/flower_photos.tgz | tar xz -C data
!ls ./data/flower_photos/<jupyter_output>daisy  dandelion  LICENSE.txt  roses  sunflowers  tulips<jupyter_text>Flower dataset from [Tensorflow tutorials](https://www.tensorflow.org/tutorials/image_retraining). Dataset contains about 3500 images in set of subfolders. The images are not split into training and validation dataset.  Load libraries<jupyter_code>import matplotlib.pyplot as plt
from pathlib import Path
import cv2
import numpy as np
from matplotlib import patches, patheffects
from keras import backend as K
from keras.models import Model 
from keras.layers import Dense, Dropout, GlobalAveragePooling2D
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from keras.applications.vgg16 import VGG16, preprocess_input, decode_predictions
from keras.optimizers import Adam, SGD
def show_img(im, figsize=None, ax=None):
    if not ax: fig,ax = plt.subplots(figsize=figsize)
    ax.imshow(im)
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)
    return ax
  
def draw_outline(o, lw):
    o.set_path_effects([patheffects.Stroke(
        linewidth=lw, foreground='black'), patheffects.Normal()])
  
def draw_text(ax, xy, txt, sz=14):
    text = ax.text(*xy, txt,
           verticalalignment='top', color='white', fontsize=sz, weight='bold')
    draw_outline(text, 1)
show_img(load_img('./data/flower_photos/daisy/1031799732_e7f4008c03.jpg'))
num_classes = 5
flowers = ['daisy','dandelion','roses','sunflowers','tulips']
SZ = 224
BS = 16<jupyter_output><empty_output><jupyter_text>Transfer learning Try to train your flowers classifier on top of of VGG 16.Hints: We have 5 different classes, so instead of binary_crossentropy and sigmoid, we are going to use categorical_crossentropy ad softmax.The dataset is small, so data augumentation may help.  To split the dataset into training an validation sets, use the validation_split keyword in ImageDataGenerator<jupyter_code>base_model = # your code here


x = base_model.output
# your code here
# you can play with amount of dropout<jupyter_output><empty_output><jupyter_text>Make sure to train only the newly added layers and compile the model:<jupyter_code># your code here<jupyter_output><empty_output><jupyter_text>Create data generators.  Hint:  Use validation_split keyword in ImageDataGenerator and subset keyword in datagen.flow_from_directory().<jupyter_code>train_datagen =  ImageDataGenerator(
    preprocessing_function=preprocess_input,
    rotation_range=35,  
    width_shift_range=0.1,  
    height_shift_range=0.1, 
    horizontal_flip=True, 
    shear_range=0.1,
    validation_split=0.1
)

train_generator = train_datagen.flow_from_directory( 
    # your code here
    subset='training'
)

validation_generator = train_datagen.flow_from_directory(
  # your code here
  subset='validation'
)<jupyter_output>Found 3306 images belonging to 5 classes.
Found 364 images belonging to 5 classes.<jupyter_text>Train your classifier and save its output to a variable.  Tou may need more epochs than 3.<jupyter_code>%%time
history = model.fit_generator(
    # your code here
)
plt.figure(figsize=(10, 7))
plt.plot(history.history['acc'])
plt.plot(history.history['val_acc']) 
plt.title('model accuracy')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend(['train', 'test'])
plt.show()<jupyter_output><empty_output><jupyter_text>Fine tuning Select layers your want to train / freeze<jupyter_code># your code here<jupyter_output><empty_output><jupyter_text>fine-tune (train) your model  Check predictions for the validation set<jupyter_code>y_true = validation_generator.classes
preds = model.predict_generator(validation_generator)
y_pred = np.argmax(preds, axis=1) # we are looking for a class with maximum score
from sklearn.metrics import confusion_matrix
import itertools

def plot_confusion_matrix(cm, classes, normalize=False, title='Confusion matrix', cmap=plt.cm.Blues):
    """
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    (This function is copied from the scikit docs.)
    """
    plt.figure(figsize=(12, 12))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=90)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
    print(cm)
    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j], horizontalalignment="center", 
                 color="white" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')<jupyter_output><empty_output><jupyter_text>Plot the confusion matrix and check which classes are missclassified most often<jupyter_code># your code here<jupyter_output><empty_output><jupyter_text>Plot some corectly and incorrectly classified images.<jupyter_code># your code here :o)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09992386752950133, 'fraction_numerical': 0.013894175866006851, 'mean_word_length': 5.354292623941959, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 40, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '394829', 'license': 'MIT', 'n_tokens_mistral': 1797, 'n_tokens_neox': 1696, 'n_words': 445, 'path': '03-flowers.ipynb', 'repo_name': 'karlafej/keras_pyconCZ', 'size': '5254'}