问题描述
我正在使用海报图像进行电影类型预测。在其中我创建了一个模型管道,我将图像路径作为输入并在管道中对其进行预处理,最后,它给了我预测(电影类型)。但是我想更改输出的格式,我已经尝试创建预测函数但它不起作用。
在这个转换器中,我将我的图像路径转换为 NumPy 数组
from sklearn.base import BaseEstimator,TransformerMixin
class RGB2GrayTransformer(BaseEstimator,TransformerMixin):
"""
Convert an array of RGB images to grayscale
"""
def __init__(self,datafile='/content/Movies-Poster_Dataset/train.csv'):
self.df = pd.read_csv(datafile)
self.df = self.df.iloc[:1000,:]
def fit(self,X,y=None):
"""returns itself"""
return self
def transform(self,y=None):
"""perform the transformation and return an array"""
l = np.empty(shape=[850,350,3])
if isinstance(X,str):
X = image.load_img(X,target_size=(img_width,img_height,3))
img = image.img_to_array(X)
img = img/255.0
img = img.reshape(1,img_width,3)
np.append(l,img)
else:
for img in X:
img = image.img_to_array(img)
img = img/255.0
img = img.reshape(1,3)
np.append(l,img)
return l
在下面,我正在创建一个 Keras 模型并预测我的结果
def get_training_model():
input_layer = tf.keras.layers.Input(shape=(350,3),name="input_layer")
c1=Conv2D(16,(3,activation='relu',input_shape = X_train[0].shape)(input_layer)
c3=Batchnormalization()(c1)
c4=MaxPool2D(2,2)(c3)
c5=Dropout(0.3)(c4)
c6=Conv2D(32,activation='relu')(c5)
c7=Batchnormalization()(c6)
c8=MaxPool2D(2,2)(c7)
c9=Dropout(0.3)(c8)
c10=Conv2D(64,activation='relu')(c9)
c11=Batchnormalization()(c10)
c12=MaxPool2D(2,2)(c11)
c13=Dropout(0.4)(c12)
c14=Conv2D(128,activation='relu')(c13)
c15=Batchnormalization()(c14)
c16=MaxPool2D(2,2)(c15)
c17=Dropout(0.5)(c16)
c18=Flatten()(c17)
c19=Dense(128,activation='relu')(c18)
c20=Batchnormalization()(c19)
c21=Dropout(0.5)(c20)
c23=Dense(128,activation='relu')(c21)
c24=Batchnormalization()(c23)
c25=Dropout(0.5)(c24)
outputs=Dense(25,activation='sigmoid')(c25)
# Create the model
model = tf.keras.models.Model(input_layer,outputs)
# Compile the model and return it
model.compile(optimizer='adam',loss = 'binary_crossentropy',metrics=['accuracy'])
return model
然后我的管道看起来像这样
HOG_pipeline = Pipeline([
('grayify',RGB2GrayTransformer()),('final',get_training_model())
])
和我得到的输出:
而我想要的输出:
使用下面的代码,我可以获得所需格式的输出,但我不知道如何将此代码放入我的管道中
top3 = np.argsort(y_prob[0])[:-4:-1]
l=[]
for i in range(3):
l.append(classes[top3[i]])
print(l)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)