# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# + [markdown] _uuid="37fd1fed10a0d3943a4f511b77e31b73d4934f18"
# # Using a Keras Convolutional Neural Network for classifying Flower Images

# + _uuid="5bb39a33e85142d77a0eaf1f91542a469b5314fd"
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

# + _uuid="b00b29f5cbd7623a2bbff66b8b680a1258200ba2"
from glob import glob
import cv2

# + [markdown] _uuid="552fec21a33a829a48c1827c6cfcb402e2e2058c"
# ## Importing images using OpenCV and Glob

# + _uuid="07207a0af8e00dde12f5841fd1e78f1d947fbeb1"
import os

# + _uuid="0879a41a779486818a977a9b41efa1f0f7c65854"
os.listdir("../input/flowers/flowers/")

# + _uuid="a46a03211f1cb583d3f75d453f1e35084afe902c"
daisy_path = glob("../input/flowers/flowers/daisy/*jpg", recursive=True)
dandi_path = glob("../input/flowers/flowers/dandelion/*jpg", recursive=True)
rose_path = glob("../input/flowers/flowers/rose/*jpg", recursive=True)
sunflower_path = glob("../input/flowers/flowers/sunflower/*jpg", recursive=True)
tulip_path = glob("../input/flowers/flowers/tulip/*jpg", recursive=True)

# + [markdown] _uuid="62ca018454586a48cd79f32f37630ea3106bf072"
# 1. Above, we have defined the paths to the flowers dataset images.
# 2. In the below step, we read image files into the dataset using OpenCV (`cv2`)

# + _uuid="51cba67ee890d1cde90bdfdb9871dc8a060aced0"
daisy_images = [cv2.imread(file) for file in daisy_path]
dandi_images = [cv2.imread(file) for file in dandi_path]
rose_images = [cv2.imread(file) for file in rose_path]
sunflower_images = [cv2.imread(file) for file in sunflower_path]
tulip_images = [cv2.imread(file) for file in tulip_path]

# + [markdown] _uuid="8601816793c951e0c6cc09eaa17a099b9bc2c722"
# Print 4 random images from the daisy_images set

# + _uuid="28191696825ac112c3cc3480649fa30ff431325f"
print("Shapes of different daisy images and what they look like\n")
def print_image(img):
    plt.figure()
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.title(img.shape)
    plt.show()

for num in np.random.choice(daisy_images, size = 4):
    print_image(num)


# + _uuid="906728d65edc86cebbc2d1f6617c65f63733e9e9"
def resize_image(image):
    return cv2.resize(image, (320,320))


# + _uuid="337bf5320074623935aa465b0889405a1e562b40"
reshaped_daisy = [resize_image(img) for img in daisy_images]
reshaped_dandi = [resize_image(img) for img in dandi_images]
reshaped_rose = [resize_image(img) for img in rose_images]
reshaped_sunflower = [resize_image(img) for img in sunflower_images]
reshaped_tulip = [resize_image(img) for img in tulip_images]

# + _uuid="a6ccf9983632519317797944887abb206de29543"
num_daisy, num_dandi, num_rose, num_sunflower, num_tulip = len(reshaped_daisy), len(reshaped_dandi), len(reshaped_rose), len(reshaped_sunflower), len(reshaped_tulip)

# + _uuid="892ad60b474cbd34b1f1c48bfe5cf68383150b36"
labels_daisy = ['daisy']*num_daisy
labels_dandi = ['dandi']*num_dandi
labels_rose = ['rose']*num_rose
labels_sunflower = ['sunflower']*num_sunflower
labels_tulip = ['tulip']*num_tulip

# + _uuid="68fe0d7c582892cc42b60e9d2694204b33c73ed9"
labels = np.concatenate((labels_daisy, labels_dandi, labels_rose, labels_sunflower, labels_tulip))

# + _uuid="848779463b0308b509cb131e5fb637286b5fdd11"
images = np.concatenate((reshaped_daisy, reshaped_dandi, reshaped_rose, reshaped_sunflower, reshaped_tulip))

# + _uuid="f95e63933f699481b86ea61790d3ba1231be7de5"
print("Images data shape:", images.shape)
print("Labels data shape:", labels.shape)

# + _uuid="69ab43d1e1d71a705b4c17199790746bcc62cd63"
n_classes = len(np.unique(labels))

# + _uuid="0965cec9597821f09b20b5a21c57707f9973948f"
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Dense, Dropout, BatchNormalization, Flatten
from keras.regularizers import l1_l2
from keras.optimizers import Adam

# + _uuid="7cceed0794270c3e61aa392f3464c77c1be026dc"
#Params

n_hidden = 50
l1, l2 = 0.15, 0.15
dropoutpc = 0.25

# + _uuid="e5fbb09171582a771ddfad6ae6c02406a0cb3769"
flw_model = Sequential([
    Conv2D(filters=10,kernel_size=(2,2), activation='relu', kernel_regularizer=l1_l2(l1, l2), strides=1, padding='same', input_shape = (320,320,3), data_format='channels_last'),
    MaxPool2D((2,2)),
    Conv2D(filters=20,kernel_size=(2,2), activation='relu', kernel_regularizer=l1_l2(l1, l2), strides=1, padding='same'),
    Conv2D(filters=20,kernel_size=(4,4), activation='relu', kernel_regularizer=l1_l2(l1, l2), strides=1, padding='same'),
    MaxPool2D((3,3)),
    Conv2D(filters=30,kernel_size=(2,2), activation='relu', kernel_regularizer=l1_l2(l1, l2), strides=1, padding='same'),
    Conv2D(filters=30,kernel_size=(4,4), activation='relu', kernel_regularizer=l1_l2(l1, l2), strides=1, padding='same'),
    MaxPool2D((2,2)),
    Conv2D(filters=40,kernel_size=(2,2), activation='relu', kernel_regularizer=l1_l2(l1, l2), strides=1, padding='same'),
    Conv2D(filters=40,kernel_size=(4,4), activation='relu', kernel_regularizer=l1_l2(l1, l2), strides=1, padding='same'),
    MaxPool2D((2,2)),
    Dense(n_hidden),
    Flatten(),
    BatchNormalization(),
    Dropout(dropoutpc),
    Dense(n_classes, activation="softmax")    
])

# + _uuid="37995a9631c2726e3e4d3ee24c734125b39bb24c"
flw_model.summary()

# + _uuid="789b91540a5a62ceedd12b14d30f4bcb312c91d6"
from sklearn.cross_validation import train_test_split
from sklearn.metrics import precision_score, recall_score, f1_score, confusion_matrix

# + _uuid="84217052faeb6fd4e09e0190d648cf8ef90dc82c"
label_dummies = pd.get_dummies(labels).as_matrix()
print(label_dummies.shape)

# + _uuid="df041fca9395978f304cc1c0dad8e4a2816de6ff"
trainX, testX, trainY_cat, testY_cat = train_test_split(images, labels, test_size = 0.2)
trainY = pd.get_dummies(trainY_cat).as_matrix()
testY = pd.get_dummies(testY_cat).as_matrix()


# + _uuid="5f0436400c6821f311a18f84acd8732dc8a5f873"
batchsize = 16
epochs = 32
lr = 1e-4
decay = 0.1 * lr

opt = Adam(lr, decay)

# + _uuid="c06cc5bfe6f3034b4250f5e5e4689e0e38c7a235"
flw_model.compile(optimizer=opt, loss="categorical_crossentropy", metrics=['acc'])

# + _uuid="b23a27ae6ad79f6a6462aa1e6d5cce34b5c1372b"
history = flw_model.fit(x = trainX, y= trainY, batch_size = batchsize, epochs = epochs, validation_split= 0.2, verbose=1)

# + _uuid="2ef03fe1873108064d875a37f9567069e6af5e0f"
pd.DataFrame(history.history)[['loss', 'val_loss']].plot(figsize = (16,4), title = "Loss")
pd.DataFrame(history.history)[['acc', 'val_acc']].plot(figsize = (16,4), title = "Accuracy")

# + _uuid="b1ee752ca0f6f00e96907eed1cf90eaf3744f9b8"
test_pred = flw_model.predict_classes(testX).reshape((len(test_pred),1))
print(test_pred.shape)

testY_labels = np.reshape(np.argmax(testY, axis=1), (-1,1))

precision_score(testY_labels, test_pred, average='macro'), recall_score(testY_labels, test_pred, average='macro'), f1_score(testY_labels, test_pred, average='macro')

# + _uuid="9a5b3725390b1dc86618027665d19e76ef69cef0"


StarcoderdataJupyterScriptsDedupFiltered