cross validation sklearn image classification

Solutions on MaxInterview for cross validation sklearn image classification by the best coders in the world

showing results for - "cross validation sklearn image classification"
Andrea
12 Oct 2018
1import numpy as np
2import tensorflow as tf
3from keras.models import Model
4from keras.layers import Input, Activation, Dense, Conv2D, MaxPooling2D, Flatten
5from keras.preprocessing.image import ImageDataGenerator
6from keras.optimizers import Adam
7from keras.callbacks import TensorBoard
8
9# Images Dimensions
10img_width, img_height = 200, 200
11
12# Data Path
13train_data_dir = 'data/train'
14validation_data_dir = 'data/validation'
15
16# Parameters
17nb_train_samples = 100
18nb_validation_samples = 50
19epochs = 50
20batch_size = 10
21
22# TensorBoard Callbacks
23callbacks = TensorBoard(log_dir='./Graph')
24
25# Training Data Augmentation
26train_datagen = ImageDataGenerator(
27    rescale=1. / 255,
28    shear_range=0.2,
29    zoom_range=0.2,
30    horizontal_flip=True)
31
32# Rescale Testing Data
33test_datagen = ImageDataGenerator(rescale=1. / 255)
34
35# Train Data Generator
36train_generator = train_datagen.flow_from_directory(
37    train_data_dir,
38    target_size=(img_width, img_height),
39    batch_size=batch_size,
40    class_mode='categorical')
41
42# Testing Data Generator
43validation_generator = test_datagen.flow_from_directory(
44    validation_data_dir,
45    target_size=(img_width, img_height),
46    batch_size=batch_size,
47    class_mode='categorical')
48
49# Feature Extraction Layer KorNet
50inputs = Input(shape=(img_width, img_height, 3))
51conv_layer = Conv2D(16, (5, 5), strides=(3,3), activation='relu')(inputs) 
52conv_layer = MaxPooling2D((2, 2))(conv_layer) 
53conv_layer = Conv2D(32, (5, 5), strides=(3,3), activation='relu')(conv_layer) 
54conv_layer = MaxPooling2D((2, 2))(conv_layer) 
55
56# Flatten Layer
57flatten = Flatten()(conv_layer) 
58
59# Fully Connected Layer
60fc_layer = Dense(32, activation='relu')(flatten)
61outputs = Dense(3, activation='softmax')(fc_layer)
62
63model = Model(inputs=inputs, outputs=outputs)
64
65# Adam Optimizer and Cross Entropy Loss
66adam = Adam(lr=0.0001)
67model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
68
69# Print Model Summary
70print(model.summary())
71
72model.fit_generator(
73    train_generator,
74    steps_per_epoch=nb_train_samples // batch_size,
75    epochs=epochs,
76    validation_data=validation_generator,
77    validation_steps=nb_validation_samples // batch_size, 
78    callbacks=[callbacks])
79
80model.save('./models/model.h5')
81model.save_weights('./models/weights.h5')
82