Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

cross-validation sklearn image classification

import numpy as np
import tensorflow as tf
from keras.models import Model
from keras.layers import Input, Activation, Dense, Conv2D, MaxPooling2D, Flatten
from keras.preprocessing.image import ImageDataGenerator
from keras.optimizers import Adam
from keras.callbacks import TensorBoard

# Images Dimensions
img_width, img_height = 200, 200

# Data Path
train_data_dir = 'data/train'
validation_data_dir = 'data/validation'

# Parameters
nb_train_samples = 100
nb_validation_samples = 50
epochs = 50
batch_size = 10

# TensorBoard Callbacks
callbacks = TensorBoard(log_dir='./Graph')

# Training Data Augmentation
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)

# Rescale Testing Data
test_datagen = ImageDataGenerator(rescale=1. / 255)

# Train Data Generator
train_generator = train_datagen.flow_from_directory(
    train_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')

# Testing Data Generator
validation_generator = test_datagen.flow_from_directory(
    validation_data_dir,
    target_size=(img_width, img_height),
    batch_size=batch_size,
    class_mode='categorical')

# Feature Extraction Layer KorNet
inputs = Input(shape=(img_width, img_height, 3))
conv_layer = Conv2D(16, (5, 5), strides=(3,3), activation='relu')(inputs) 
conv_layer = MaxPooling2D((2, 2))(conv_layer) 
conv_layer = Conv2D(32, (5, 5), strides=(3,3), activation='relu')(conv_layer) 
conv_layer = MaxPooling2D((2, 2))(conv_layer) 

# Flatten Layer
flatten = Flatten()(conv_layer) 

# Fully Connected Layer
fc_layer = Dense(32, activation='relu')(flatten)
outputs = Dense(3, activation='softmax')(fc_layer)

model = Model(inputs=inputs, outputs=outputs)

# Adam Optimizer and Cross Entropy Loss
adam = Adam(lr=0.0001)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])

# Print Model Summary
print(model.summary())

model.fit_generator(
    train_generator,
    steps_per_epoch=nb_train_samples // batch_size,
    epochs=epochs,
    validation_data=validation_generator,
    validation_steps=nb_validation_samples // batch_size, 
    callbacks=[callbacks])

model.save('./models/model.h5')
model.save_weights('./models/weights.h5')
Comment

PREVIOUS NEXT
Code Example
Python :: python time.sleep slow 
Python :: poython inl linrt dor loop 
Python :: python split files into even sets of folders 
Python :: How to make boxplot using seaborne 
Python :: use an async check function for discord.py wait_for? 
Python :: mechanize python #4 
Python :: how to code discord bot 8ball python 
Python :: how to get data from multiple tables in django 
Python :: comment interpreter tuple python avec valeur unique 
Python :: flask event source 
Python :: python dynamic csvnfile joining 
Python :: bson to dataframe pandas 
Python :: how does gas exchange happen in the alveoli 
Python :: Jupyter to access jupyter notebook on virtualbox guest through browser in windows host 
Python :: str vs rper in python 
Python :: python discord bot create role 
Python :: square root in python numpy 
Python :: nbt python 
Python :: how parse date python no specific format 
Python :: Command to import Required, All, Length, and Range from voluptuous 
Python :: how to get class names in predict_proba 
Python :: install Social Auth App Flask 
Python :: linkedin python test 
Python :: *9c0xxbz] 
Python :: attach short list to pandas dataframe with filler 
Python :: Python NumPy ndarray flatten Function Example 02 
Python :: tqdm start bar at 
Python :: Python NumPy column_stack Function Example with 2d array 
Python :: creating a variable bound to a set python 
Python :: python model feature importance 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =