overriding predict function of custom transformer in sklearn

Viewed 201

I am working on a movie genre prediction using poster images. In which I have created a model pipeline where I am giving image path as an input and preprocessing it in the pipeline and at last, it is giving me the prediction(movie genre). But I want to change the format of the output, I have tried creating the predict function but it's not working.

In this transformer, I am converting my image path into the NumPy array

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, X, y=None):
      """perform the transformation and return an array"""
      l = np.empty(shape=[850,350,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, img_height, 3)
         np.append(l,img)
      else:
         for img in X:
            img = image.img_to_array(img)
            img = img/255.0
            img = img.reshape(1, img_width, img_height, 3)
            np.append(l,img)
      return l

In the below, I am creating a Keras model and predicting my result

def get_training_model():
 input_layer = tf.keras.layers.Input(shape=(350,350,3),name="input_layer")
 c1=Conv2D(16, (3,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, (3,3), activation='relu')(c5)
 c7=BatchNormalization()(c6)
 c8=MaxPool2D(2,2)(c7)
 c9=Dropout(0.3)(c8)
 c10=Conv2D(64, (3,3), activation='relu')(c9)
 c11=BatchNormalization()(c10)
 c12=MaxPool2D(2,2)(c11)
 c13=Dropout(0.4)(c12)
 c14=Conv2D(128, (3,3), 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

And then my pipeline is looking something like this

HOG_pipeline = Pipeline([
('grayify', RGB2GrayTransformer()),
('final',get_training_model())
])

and the output I am getting :

enter image description here

whereas the output I want :

enter image description here

Using the below code I can get my output in the desired format but I don't know how to fit this code in my pipeline

top3 = np.argsort(y_prob[0])[:-4:-1]
l=[] 
for i in range(3):
   l.append(classes[top3[i]])
print(l)
0 Answers
Related