This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%% 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Model Structure | |
from keras.utils import plot_model | |
plot_model(model, to_file='model_{}.png'.format(timestr),show_shapes=True, show_layer_names=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% 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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#%% 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. |
OlderNewer