New Ribbon
Canva+AI創意設計與品牌應用250招:從商業技巧、社群祕技到AI圖文影音特效 快快樂樂學威力導演2024.影音剪輯與AI精彩創作 文淵閣工作室祝福大家新年快樂.龍年吉祥 Power Automate自動化超效率工作術 Midjourney AI圖像魔導書:搭配ChatGPT魔法加倍 超人氣FB+IG+LINE社群經營與行銷力(第二版) 翻倍效率工作術:不會就太可惜的 Excel × ChatGPT 自動化應用 AppInventor2零基礎入門班中文版(第六版) Python零基礎入門班(第四版) C語言學習聖經 用Canva設計超快超質感:平面、網頁、電子書、簡報、影片製作與AI繪圖最速技 PHP8/MySQL網頁程式設計自學聖經 翻倍效率工作術 - 不會就太可惜的Power BI大數據視覺圖表設計與分析(第三版) 社群經營一定要會的影音剪輯與動畫製作術 Notion高效管理250招:筆記×資料庫×團隊協作,數位生活與工作最佳幫手 Office 2021高效實用範例必修16課(附500分鐘影音教學/範例檔) Excel自學聖經(第二版):從完整入門到職場活用的技巧與實例大全 網路開店×拍賣王:蝦皮來了(第二版) 專家都在用的Google最強實戰:表單、文件、試算、簡報、遠距與線上會議 超人氣 Instagram 視覺行銷力(第二版):小編不敗,經營 IG 品牌人氣王的 120 個秘技!

 

  機器學習與深度學習特訓班

蘇


更新時間:2022/2/23 上午 10:03:20

 

執行Keras_Mnist_MLP.py發生
'Sequential' object has no attribute 'predict_classes'
請問如何修改?
另外:
1.之前跑過沒問題,難道套件內容有改?
2.有看到其他讀者也問過同樣問題,但沒看到解答,只好再貼一次,抱歉!

文淵閣工作室

文淵閣工作室
更新時間:2022/2/24 上午 09:18:15

 

您好:
感謝您的支持。

predict_classes 現已棄用,請改用下列的程式碼代替。
# prediction=model.predict_classes(test_feature_normalize)
predict=model.predict(test_feature_normalize)
prediction=np.argmax(predict,axis=1)

蘇


更新時間:2022/2/24 下午 11:32:34

 

感謝!已正常了!

蘇


更新時間:2022/3/16 下午 03:56:15

 

同樣程式在anaconda中OK,但在COLAB中有以下問題:
import numpy as np
np.random.seed(10)
import matplotlib.pyplot as plt
from keras.models import load_model
import glob,cv2

def show_images_labels_predictions(images,labels,
                                  predictions,start_id,num=10):
    plt.gcf().set_size_inches(12, 14)
    if num>25: num=25
    for i in range(0, num):
        ax=plt.subplot(5,5, 1+i)
        #顯示黑白圖片
        ax.imshow(images[start_id], cmap='binary')
        
        # 有 AI 預測結果資料, 才在標題顯示預測結果
        if( len(predictions) > 0 ) :
            title = 'ai = ' + str(predictions[i])
            # 預測正確顯示(o), 錯誤顯示(x)
            title += (' (o)' if predictions[i]==labels[i] else ' (x)')
            title += '\nlabel = ' + str(labels[i])
        # 沒有 AI 預測結果資料, 只在標題顯示真實數值
        else :
            title = 'label = ' + str(labels[i])
            
        # X, Y 軸不顯示刻度    
        ax.set_title(title,fontsize=12)
        ax.set_xticks([]);ax.set_yticks([])        
        start_id+=1
    plt.show()
    
#建立測試特徵集、測試標籤     
files = glob.glob("/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata\*.jpg" )
test_feature=[]
test_label=[]
for file in files:
    img=cv2.imread(file)
    img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  #灰階    
    _, img = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY_INV) #轉為反相黑白
    test_feature.append(img)
    label=file[10:11]  # "imagedata\1.jpg" 第10個字元1為label
    test_label.append(int(label))
  
test_feature=np.array(test_feature) # 串列轉為矩陣
test_label=np.array(test_label)     # 串列轉為矩陣

#將 Features 特徵值換為 784個 float 數字的 1 維向量
test_feature_vector = test_feature.reshape(len( test_feature), 784).astype('float32')

#Features 特徵值標準化
test_feature_normalize = test_feature_vector/255

#從 HDF5 檔案中載入模型
print("載入模型 Mnist_mlp_model.h5")
model = load_model('Mnist_mlp_model.h5')
    
#預測
#prediction=model.predict_classes(test_feature_normalize)#已棄用
predict=model.predict(test_feature_normalize)
prediction=np.argmax(predict,axis=1)

#顯示圖像、預測值、真實值
show_images_labels_predictions(test_feature,test_label,prediction,0,len(test_feature))

載入模型 Mnist_mlp_model.h5
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-de705c155693> in <module>()
     57 #預測
     58 #prediction=model.predict_classes(test_feature_normalize)#已棄用
---> 59 predict=model.predict(test_feature_normalize)
     60 prediction=np.argmax(predict,axis=1)
     61

1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)
   1995             callbacks.on_predict_batch_end(end_step, {'outputs': batch_outputs})
   1996       if batch_outputs is None:
-> 1997         raise ValueError('Unexpected result of `predict_function` '
   1998                          '(Empty batch_outputs). Please use '
   1999                          '`Model.compile(..., run_eagerly=True)`, or '

ValueError: Unexpected result of `predict_function` (Empty batch_outputs). Please use `Model.compile(..., run_eagerly=True)`, or `tf.config.run_functions_eagerly(True)` for more information of where went wrong, or file a issue/bug to `tf.keras`.

蘇


更新時間:2022/3/16 下午 04:38:28

 

將圖檔路徑修正後,得以下訊息:
ValueError                                Traceback (most recent call last)
<ipython-input-18-8d3a725e6db9> in <module>()
     40     test_feature.append(img)
     41     label=file[10:11]  # "imagedata\1.jpg" 第10個字元1為label
---> 42     test_label.append(int(label))
     43
     44 test_feature=np.array(test_feature) # 串列轉為矩陣

ValueError: invalid literal for int() with base 10: 'r'

文淵閣工作室

文淵閣工作室
更新時間:2022/3/17 上午 11:27:07

 

一、Colab 路徑,請使用 「/」符號。
files = glob.glob("/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/*.jpg" )

二、請使用 print(file) 先了解路徑內容,
for file in files:
    print(file)
    img=cv2.imread(file)
    #img=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  #灰階    
    #_, img = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV) #轉為反相黑白
    #test_feature.append(img)
    #label=file[10:11]  # 例如:"imagedata\1.jpg" 第10個字元1為label
    #test_label.append(int(label)

我的圖片路徑顯示如下:
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/6_1.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/9_3.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/7.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/2_1.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/9_1.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/9_2.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/3_1.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/5_1.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/4_1.jpg
/content/drive/MyDrive/Colab Notebooks/機器學習與深度學習特訓班/imagedata/1.jpg

例如:6_1.jpg 這張圖的標籤是 6,因此您應改用「/」字元分割後,取得最後的字串即 6_1.jpg,再取這個字串 (6_1.jpg) 的第 1 個字元 6。




 

 

Re:機器學習與深度學習特訓班

請輸入姓名。

已超出字元數目的最大值。


請輸入電子郵件。

格式無效。


請輸入內容。