我正在使用 VGG16 預訓練模型解決多視圖分類問題。就我而言,我有 4 個視圖作為我的輸入,它們的大小為 (64,64,3)。但是 VGG16 使用的輸入大小為 (224,224,3)。
現在為了解決這個問題,我應該創建自己的資料加載器,而不是使用快速的內置方法,如keras load_img() 或openCV imread()。所以我用普通的numpy陣列來做這一切。
我正在嘗試將輸入的形狀從 64x64 調整為 224X224。但我無法做到這一點,它不斷拋出一個或另一個錯誤。這是我的資料加載器代碼:
def data_loader(dataframe, classDict, basePath, batch_size=16):
while True:
x_batch = np.zeros((batch_size, 4, 64, 64, 3)) #Create a zeros array for images
y_batch = np.zeros((batch_size, 20)) #Create a zeros array for classes
for i in range(0, batch_size):
rndNumber = np.random.randint(len(dataframe))
*images, class_id = dataframe.iloc[rndNumber]
for j in range(4):
x_batch[i,j] = plt.imread(os.path.join(basePath, images[j])) / 255.
# x_batch[i,j] = x_batch[i,j].resize(1, 224, 224, 3) #<--- Try(1)
class_id = classDict[class_id]
y_batch[i, class_id] = 1.0
# yield {'image1': np.resize(x_batch[:, 0],(batch_size, 224, 224, 3)), #<--- Try(2)
# 'image2': np.resize(x_batch[:, 1],(1, 224, 224, 3)),
# 'image3': np.resize(x_batch[:, 2],(1, 224, 224, 3)),
# 'image4': np.resize(x_batch[:, 3],(1, 224, 224, 3)) }, {'class_out': y_batch} #'yield' is a keyword that is used like return, except the function will return a generator"
yield {'image1': x_batch[:, 0],
'image2': x_batch[:, 1],
'image3': x_batch[:, 2],
'image4': x_batch[:, 3], }, {'class_out': y_batch}
## Testing the data loader
example, lbl= next(data_loader(df_train, classDictTrain, basePath))
print(example['image1'].shape) #example['image1'][0].shape
print(lbl['class_out'].shape)
我已經多次嘗試調整影像大小。我在下面列出了它們以及每次嘗試時收到的錯誤訊息:
Try(1) : Using
x_batch[i,j] = x_batch[i,j].resize(1, 224, 224, 3)>> Error: ValueError: cannot resize this array: it does not own its dataTry(2) :使用
yield {'image1': np.resize(x_batch[:, 0],(batch_size, 224, 224, 3)), ....... }>> 輸出形狀是 (16, 224, 224, 3) 看起來不錯,但是當我繪制它時,結果是這樣的影像
我需要像這樣尺寸更大的原始影像

請告訴我我做錯了什么,我該如何解決?
uj5u.com熱心網友回復:
如果我正確理解您的問題,您有一個 64x64 的影像,并且您想將其放大到 224x224 的解析度。請注意,后一種解析度包含更多像素,您不能簡單地強制重塑,因為原始影像的像素更少。
您必須對影像進行上采樣,生成丟失的像素。您可以嘗試的一個工具是PIL Resize 函式,它可以與不同的重采樣濾波器一起使用。
據我所知,numpy 不容易支持升級過濾器。查看這篇文章以了解如何將 PIL 影像轉換為 numpy 陣列,然后您就可以開始了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/505711.html
上一篇:如何提取給定影像中的白環
下一篇:發票表設計
