Commit
·
e627823
1
Parent(s):
6501c46
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from keras.models import Model
|
| 3 |
+
from tensorflow.keras.optimizers import Adam
|
| 4 |
+
from keras.applications.vgg16 import VGG16, preprocess_input
|
| 5 |
+
from keras.preprocessing.image import ImageDataGenerator
|
| 6 |
+
from keras.callbacks import ModelCheckpoint, EarlyStopping
|
| 7 |
+
from keras.layers import Dense, Dropout, Flatten
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
import numpy as np
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
BATCH_SIZE = 64
|
| 14 |
+
|
| 15 |
+
train_generator = ImageDataGenerator(rotation_range=90,
|
| 16 |
+
brightness_range=[0.1, 0.7],
|
| 17 |
+
width_shift_range=0.5,
|
| 18 |
+
height_shift_range=0.5,
|
| 19 |
+
horizontal_flip=True,
|
| 20 |
+
vertical_flip=True,
|
| 21 |
+
validation_split=0.15,
|
| 22 |
+
preprocessing_function=preprocess_input) # VGG16 preprocessing
|
| 23 |
+
|
| 24 |
+
test_generator = ImageDataGenerator(preprocessing_function=preprocess_input) # VGG16 preprocessing
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
train_data_dir = '/kaggle/input/pic-a-plant2/DBa4/DBa/train'
|
| 31 |
+
test_data_dir = '/kaggle/input/pic-a-plant2/DBa4/DBa/test'
|
| 32 |
+
|
| 33 |
+
class_subset = sorted(os.listdir(train_data_dir))[:] # Using only the first 10 classes
|
| 34 |
+
|
| 35 |
+
traingen = train_generator.flow_from_directory(train_data_dir,
|
| 36 |
+
target_size=(150, 150),
|
| 37 |
+
class_mode='categorical',
|
| 38 |
+
classes=class_subset,
|
| 39 |
+
subset='training',
|
| 40 |
+
batch_size=BATCH_SIZE,
|
| 41 |
+
shuffle=True,
|
| 42 |
+
seed=42)
|
| 43 |
+
|
| 44 |
+
validgen = train_generator.flow_from_directory(train_data_dir,
|
| 45 |
+
target_size=(150, 150),
|
| 46 |
+
class_mode='categorical',
|
| 47 |
+
classes=class_subset,
|
| 48 |
+
subset='validation',
|
| 49 |
+
batch_size=BATCH_SIZE,
|
| 50 |
+
shuffle=True,
|
| 51 |
+
seed=42)
|
| 52 |
+
|
| 53 |
+
testgen = test_generator.flow_from_directory(test_data_dir,
|
| 54 |
+
target_size=(150, 150),
|
| 55 |
+
class_mode=None,
|
| 56 |
+
classes=class_subset,
|
| 57 |
+
batch_size=1,
|
| 58 |
+
shuffle=False,
|
| 59 |
+
seed=42)
|