Skip to content

Instantly share code, notes, and snippets.

Avatar

aniruddha27

  • New Delhi
View GitHub Profile
View folder_structure.py
import pandas as pd
import os
import shutil
from sklearn.model_selection import train_test_split
# Home directory
home_path = r'C:/Users/Dell/Desktop/Analytics Vidhya/ImageDataGenerator/emergency_vs_non-emergency_dataset/emergency_vs_non-emergency_dataset'
# Create train and validation directories
train_path = os.path.join(home_path,'train')
View train_test_images.py
# Images
train_images = df_train.loc[:,'image_name']
train_labels = df_train.loc[:,'category']
test_images = df_test.loc[:,'image_name']
test_labels = df_test.loc[:,'category']
# Train images
x_train = []
for i in train_images:
View non_augment_model.py
# Compile
optim = RMSprop(learning_rate=0.00001)
model.compile(loss='categorical_crossentropy',
optimizer=optim,
metrics=['accuracy'])
# Fit
history = model.fit(x_train,y_train,
epochs=25,
validation_data=(x_test,y_test),
batch_size=32,
View train_test_split.py
# Split data into train-test data sets
X = df.loc[:,'image_names']
y = df.loc[:,'emergency_or_not']
# Split
train_x, val_x, train_y, val_y = train_test_split(X, y,
test_size = 0.1,
random_state = 27,
stratify=y)
View model_fit_generator_methods.py
# Augmenting on the fly with fit_generator()
# Directly use .flow()
model.fit_generator(datagen.flow(x_train, y_train, batch_size=batch_size),
epochs=epochs, # one forward/backward pass of training data
steps_per_epoch=x_train.shape[0]//batch_size, # number of images comprising of one epoch
validation_data=(x_test, y_test), # data for validation
validation_steps=x_test.shape[0]//batch_size)
# or use iterator from .flow_from_directory()
View model_fit_generator.py
# Epochs
epochs = 25
# Batch size
batch_size = 32
history = model.fit(train_datagen.flow(x_train,y_train,
batch_size=batch_size,
seed=27,
shuffle=False),
epochs=epochs,
View model_augment_flow.py
# Augmentation
train_datagen = ImageDataGenerator(rotation_range=5, # rotation
width_shift_range=0.2, # horizontal shift
zoom_range=0.2, # zoom
horizontal_flip=True, # horizontal flip
brightness_range=[0.2,0.8]) # brightness
View model_datagen.py
# Data generator
# For train data
train_datagen = ImageDataGenerator(rescale=1.0/255,
rotation_range=10,
width_shift_range=0.2,
height_shift_range=0.2,
horizontal_flip=True,
brightness_range=[0.4,0.9],
zoom_range=0.2)
View model_building.py
# Model architechture
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(224, 224, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
View load_pretrained_model.py
from keras.models import Sequential
from keras.layers import Dropout, Flatten, Dense, Conv2D, MaxPooling2D, BatchNormalization
from keras.optimizers import SGD
import keras
from sklearn.model_selection import train_test_split
import cv2
You can’t perform that action at this time.