iT邦幫忙

0

如何同時訓練數值與影像

最近一直被同一個問題困擾 想詢問一下
如果同時有照片又有數值 怎麼一起訓練模型?
因為照片轉成RPG矩陣後會變成很大的矩陣
不知道該怎麼加入變數列 一起成為輸入資料進去模型

csv檔裡面含有照片路徑、性別、年齡等數值
照片切割成50x50,照片總共有100張
那進去訓練的資料大小就會是(100,50,50,3)
要怎麼再加入excel上的數值資料(像是年紀啊、性別啊)一起訓練啊

數據使用:https://www.kaggle.com/andrewmvd/ocular-disease-recognition-odir5k

讀取函式庫

import pandas as pd
import numpy as np
from tensorflow.keras.applications.vgg16 import VGG16
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.vgg16 import preprocess_input
from tensorflow.keras import layers
from tensorflow.keras.models import Model
%pylab inline
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from tensorflow.keras.applications.vgg16 import preprocess_input
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout, InputLayer
from keras.models import Sequential
from keras import optimizers
from imblearn.over_sampling import RandomOverSampler
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.over_sampling import SMOTE

讀取csv資料

data = pd.read_csv(r"/content/drive/MyDrive/full_df.csv")
data.head()

設定Y

df_label = data[['N','D','G','C','A','H','M','O']]
y = np.array(df_label)

*** 影像模型 ***
讀取照片並轉成RPG (6392, 50, 50, 3)

# Read images and merge them into the training data X.
len_data = len(data)
X = np.array([])
path = '/content/drive/MyDrive/ODIR-5K/ODIR-5K/Training Images/'
for i in range(len_data):
  input_path = path + data['filename'][i]
  # Load image and Resize into (50, 50) 
  img = image.load_img(input_path, target_size=(50, 50))
  # Add one dimension and Become to (1, 224, 224, 3), which the last dimension is color.
  img2 = image.img_to_array(img)
  img2 = np.expand_dims(img2, axis=0)
  #print(img2.shape)
  if len(X.shape) == 1:
      X = img2
  else:
      # Combine the pixels of all images
      X = np.concatenate((X, img2), axis=0)

X = preprocess_input(X)

使用VGG16模型訓練照片

# Set VGG16 Model
base_model = VGG16(weights='imagenet', include_top=False)

# Connect My Layer
x = base_model.output
#x = layers.Flatten()(x)
x = layers.GlobalAveragePooling2D()(x)
#x = layers.Dropout(0.2)(x)
x = layers.Dense(256, activation='relu')(x)
#x = layers.Dropout(0.2)(x)
x = layers.Dense(64, activation='relu')(x)
x = layers.Dense(7, activation='sigmoid')(x)

# Set New Model inputs/outputs
model = Model(inputs=base_model.input, outputs=x)

# Original layers of VGG16 are not retrained
for layer in base_model.layers:
    layer.trainable = False
    
model.compile(optimizer='adam', loss='binary_crossentropy')

訓練照片模型

pic_output = model.fit(X, y, epochs=5, validation_split=0.2, verbose=2)

*** 數值模型 ***
資料輸入格式是 (6392, 3)

# X:病人性別
# 把Patient Sex轉成dummy code
label_encoder = preprocessing.LabelEncoder()
data['Patient Sex'] = label_encoder.fit_transform(data["Patient Sex"])

# X:病人年紀
data['Patient Age']

# X:眼睛左邊/右邊
for i in range(len_data):
  if 'right' in data['filename'][i]:
    data.loc[i,'side'] = 0
  else:
    data.loc[i,'side'] = 1
    
# 數值部分的dataframe
X_Numerical = data[['Patient Sex','Patient Age','side']]
X_Numerical_array = np.array(X_Numerical)

數值使用KNN模型(?)

knn = KNeighborsClassifier()
numerical_output = knn.fit(X_Numerical_array,y)

*** 我想要合併,但會出錯 ***

output_combined = concatenate([pic_output, numerical_output])

影像模型的參考網站大概如下,模型是參考他的,但因為自己還有數值資料,想加入一起分析,不知道該怎麼做,所以來求助
https://ithelp.ithome.com.tw/m/articles/10237020?sc=iThomeR

再麻煩指教!萬分感激!

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答