Skip to content

Instantly share code, notes, and snippets.

@MortisHuang
MortisHuang / Find_files.py
Last active February 13, 2019 04:17
Read and Filter the images from internet
import os
base_path = r'C:\Users\Mortis\Documents\Python Scripts\PetImages' #在路徑前加上r是為了標示該字串全部都只是字元,沒有特殊的指令
for root, dirs, files in os.walk(base_path):
for file in files:
if file.endswith(".jpg"):
print(os.path.join(root, file))
import os
base_path = r'C:\Users\Mortis\Documents\Python Scripts\PetImages'
for root, dirs, files in os.walk(base_path):
for file in files:
if file.endswith(".jpg"):
filename = os.path.join(root, file)
file_size = os.path.getsize(filename)
if file_size < 2048:
print("{} maybe a bad image".format(filename))
import os
import numpy as np
from PIL import Image
size = (128,128)
cat_img_list=[]
dog_img_list=[]
base_path = r'C:\Users\Mortis\Documents\Python Scripts\PetImages'
for root, dirs, files in os.walk(base_path):
for file in files:
import random
temp = list(zip(img_arr, img_label))
random.shuffle(temp)
img_arr, img_label = zip(*temp)
img_arr=np.asarray(img_arr)
img_label=np.asarray(img_label)
from sklearn.model_selection import train_test_split
train_data, test_data, train_label, test_label = train_test_split(img_arr, img_label, test_size=0.2, random_state=42)
#%% Create Model
from keras.models import Sequential
from keras.layers import Dense, SpatialDropout2D, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.optimizers import SGD,RMSprop
from keras.callbacks import EarlyStopping
# Generate model
model = Sequential()
# Model Structure
from keras.utils import plot_model
plot_model(model, to_file='model_{}.png'.format(timestr),show_shapes=True, show_layer_names=True)
%% Training and saving
history = model.fit(train_data, train_label, batch_size=64, epochs=100,shuffle=True, validation_split=0.2,callbacks=[EStop])
import time
timestr = time.strftime("%Y%m%d_%H%M%S")
model.save('catdog_model_{}.h5'.format(timestr))
# Training History
import collections
import pandas as pd
hist = history.history
for key, val in hist.items(): # Count the number of epoch
numepo = len(np.asarray(val))
break
hist = collections.OrderedDict(hist)
pd.DataFrame(hist).to_excel('model_{}_history.xlsx'.format(timestr), index=True)
#%% Confusion Matrix
import itertools
from sklearn.metrics import confusion_matrix
def plot_confusion_matrix(cm, classes_x,classes_y,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
This function prints and plots the confusion matrix.