ccwu0918 commited on
Commit
49c4bcc
·
1 Parent(s): 282c50c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -63
app.py CHANGED
@@ -12,8 +12,7 @@ from tensorflow.keras.applications.resnet_v2 import preprocess_input
12
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
13
 
14
  # 金門具有代表性的栗喉蜂虎、藍孔雀、戴勝、鱟及歐亞水獺五種物種。我們來挑戰五種類別總共用五十張照片, 看能不能打造一個神經網路學會辨識這五種類別。
15
-
16
- # 1. 讀入栗喉蜂虎、藍孔雀、戴勝、鱟及歐亞水獺資料圖檔
17
  image_folders = ['Merops_philippinus', 'pavo_cristatus', 'Upupa_epops', 'King_Crab', 'otter']
18
 
19
  # 為了後面的需要,我們將五種類別照片的答案用 `labels` 呈現
@@ -23,71 +22,13 @@ num_classes = len(labels)
23
 
24
  base_dir = './classify_image/'
25
 
26
- thedir = base_dir + image_folders[0]
27
- os.listdir(thedir)
28
-
29
- data = []
30
- target = []
31
- for i in range(5):
32
- thedir = base_dir + image_folders[i]
33
- image_fnames = os.listdir(thedir)
34
- for theimage in image_fnames:
35
- if theimage == ".git" or theimage == ".ipynb_checkpoints":
36
- continue
37
- img_path = thedir + '/' + theimage
38
- img = load_img(img_path , target_size = (256,256))
39
- x = img_to_array(img)
40
- data.append(x)
41
- target.append(i)
42
-
43
- # 2. 針對資料圖檔使用 ResNet 進行預處理
44
- x_train = preprocess_input(data)
45
-
46
- y_train = to_categorical(target, num_classes)
47
-
48
- # 3. 用 ResNet50 打造我們的神經網路
49
- resnet = ResNet50V2(include_top=False, pooling="avg")
50
-
51
- # 再來就是正式打造我們遷移學習版的函數學習機! 可以發現我們只是加入了最後
52
- model = Sequential()
53
- model.add(resnet)
54
- model.add(Dense(5, activation='softmax')) # 這裡的 5 表示,輸出結果為 5 個類別
55
-
56
- # 我們是遷移式學習, 原本 ResNet 的部份我們當然沒有重新訓練的意思。於是就設這邊不需要訓練。
57
- resnet.trainable = False
58
 
59
- # 欣賞我們的神經網路
60
- model.summary()
61
-
62
- # 組裝我們的函數學習機
63
- # 這裡我們用分類時非常標準的 categorical_crossentropy, 順便試試有名的 adam 學習法,當然也可以試試 sgd 看效果如何。
64
-
65
- model.compile(loss='categorical_crossentropy',
66
- optimizer='adam',
67
- metrics=['accuracy'])
68
-
69
- # 我們可以發現原來有超過兩千萬個參數, 經我們借來以後, 只有 1,0245 個參數要調。
70
-
71
- # 4. 訓練 fit
72
- # 這裡我們全部的資料也只有 50 筆, 所以 batch_size 就選擇 25 了,同時訓練的回合數設定為 10 回合
73
- model.fit(x_train, y_train, batch_size=25, epochs=10)
74
-
75
- # 5. 預測
76
- # 我們先用 model.evaluate 看一下模型表現得如何
77
- loss, acc = model.evaluate(x_train, y_train)
78
- print(f"Loss: {loss}")
79
- print(f"Accuracy: {acc}")
80
-
81
- # 6. 將訓練完成的模型儲存起來,可供日後直接略過訓練直接載入訓練完成的模型進行辨識。
82
- model.save('my_cnn_model.h5')
83
-
84
- # 7. 載入並檢視訓練完成的模型。
85
  model = load_model('my_cnn_model.h5') # Loading the Tensorflow Saved Model (PB)
86
  print(model.summary())
87
 
88
- # 8. 用 gradio 打造栗喉蜂虎、藍孔雀、戴勝、鱟及歐亞水獺辨識 Web App!
89
-
90
  # 注意現在主函數做辨識只有五個種類。而且是使用我們自行訓練的 model!
 
91
  def classify_image(inp):
92
  inp = inp.reshape((-1, 256, 256, 3))
93
  inp = preprocess_input(inp)
@@ -108,7 +49,8 @@ for i in range(num_classes):
108
  continue
109
  sample_images.append(base_dir + image_folders[i] + '/' + file)
110
 
111
- # 最後,將所有東西組裝在一起,就大功告成了!
 
112
  iface = gr.Interface(fn=classify_image,
113
  inputs=image,
114
  outputs=label,
 
12
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
13
 
14
  # 金門具有代表性的栗喉蜂虎、藍孔雀、戴勝、鱟及歐亞水獺五種物種。我們來挑戰五種類別總共用五十張照片, 看能不能打造一個神經網路學會辨識這五種類別。
15
+ # 讀入栗喉蜂虎、藍孔雀、戴勝、鱟及歐亞水獺資料圖檔
 
16
  image_folders = ['Merops_philippinus', 'pavo_cristatus', 'Upupa_epops', 'King_Crab', 'otter']
17
 
18
  # 為了後面的需要,我們將五種類別照片的答案用 `labels` 呈現
 
22
 
23
  base_dir = './classify_image/'
24
 
25
+ # 載入並檢視訓練完成的模型。
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  model = load_model('my_cnn_model.h5') # Loading the Tensorflow Saved Model (PB)
28
  print(model.summary())
29
 
 
 
30
  # 注意現在主函數做辨識只有五個種類。而且是使用我們自行訓練的 model!
31
+
32
  def classify_image(inp):
33
  inp = inp.reshape((-1, 256, 256, 3))
34
  inp = preprocess_input(inp)
 
49
  continue
50
  sample_images.append(base_dir + image_folders[i] + '/' + file)
51
 
52
+ # 最後,將所有東西組裝在一起,就大功告成了!
53
+
54
  iface = gr.Interface(fn=classify_image,
55
  inputs=image,
56
  outputs=label,