In [2]:
import os
import time
import numpy as np
import h5py
import matplotlib.pyplot as plt
import scipy
from PIL import Image
from scipy import ndimage
import cv2
from dnn_app_utils_v2 import *
%matplotlib inline
plt.rcParams['figure.figsize'] = (5.0, 4.0) # set default size of plots
plt.rcParams['image.interpolation'] = 'nearest'
plt.rcParams['image.cmap'] = 'gray'
%load_ext autoreload
%autoreload 2
In [2]:
# 載入訓練好的權重
parameters_filePath = os.path.join("car_vehicle_automobile_truck_van_lorry", "dataset", "car_dnn_9_Layers_64x8_r2_i1000_i3000.npy")
npload = np.load(parameters_filePath)
parameters = npload.item()
In [3]:
del npload
In [4]:
# 測試
parameters.keys()
Out[4]:
In [5]:
# 測試
parameters['W1']
Out[5]:
In [17]:
my_label_y = [0] # the true class of your image (1 -> car, 0 -> non-car)
num_px = 64
dirPath = os.path.join("car_vehicle_automobile_truck_van_lorry", "test_dnn_0") # test_dnn_0 test_dnn_images
listDir = os.listdir(dirPath)
listfileName = []
listCost = []
for image in listDir:
test_filePath = os.path.join(dirPath, image)
if os.path.isfile(test_filePath):
image_load = cv2.imread(test_filePath)
image_load = cv2.cvtColor(image_load, cv2.COLOR_BGR2RGB)
my_image = cv2.resize(image_load, (num_px, num_px), interpolation=cv2.INTER_LINEAR)
my_image = my_image.reshape(num_px * num_px * 3, 1)
my_image = my_image/255.
my_predicted_image = predict(my_image, my_label_y, parameters)
listfileName.append(image)
listCost.append(my_predicted_image.item())
#plt.imshow(image)
#print ("y = " + str(np.squeeze(my_predicted_image)) )
else:
print("找不到 {} 檔案!!".format(my_image))
In [18]:
sum = 0
for i in range(len(listCost)):
print("file:{:18} \t Accuray:{} ".format(listfileName[i], listCost[i]))
sum += listCost[i]
In [19]:
sum / len(listAccuracy) # 1
Out[19]:
In [20]:
1 - (sum / len(listAccuracy)) # 0
Out[20]:
In [11]:
image = "Donald-Trump.jpg"
dirPath = os.path.join("car_vehicle_automobile_truck_van_lorry", "test_dnn_images") # test_dnn_0 test_dnn_images
my_label_y = [0] # the true class of your image (1 -> car, 0 -> non-car)
num_px = 64
test_filePath = os.path.join(dirPath, image)
if os.path.isfile(test_filePath):
image_load = cv2.imread(test_filePath)
image_load = cv2.cvtColor(image_load, cv2.COLOR_BGR2RGB)
my_image = cv2.resize(image_load, (num_px, num_px), interpolation=cv2.INTER_LINEAR)
my_image = my_image.reshape(num_px * num_px * 3, 1)
my_image = my_image/255.
my_predicted_image = predict(my_image, my_label_y, parameters)
plt.imshow(image_load)
print ("y = " + str(np.squeeze(my_predicted_image)) )
else:
print("找不到 {} 檔案!!".format(image))
In [4]:
with open(os.path.join("car_vehicle_automobile_truck_van_lorry", "dataset", "car_dnn_9_Layers_64x8_r2_i1000_i3000_costsList")) as f:
readfile = f.readlines()
plt.plot(readfile)
plt.ylabel('cost')
plt.xlabel('iterations')
plt.savefig(filename=os.path.join("car_vehicle_automobile_truck_van_lorry", "dataset", "car_dnn_9_Layers_64x8_r2_i1000_i3000.png"))
plt.show()
In [ ]: