McIllyaz
McIllyaz

Reputation: 9

Why do I keep getting Name Error: name 'model' is not defined

import numpy as np

from google.colab import files

from keras.preprocessing import image

from matplotlib import pyplot as plt

uploaded = files.upload()

for fn in uploaded.keys():

  path = fn

  img = image.load_img(path, target_size = (150, 150))

  imgplot = plt.imshow(img)

  x = image.img_to_array(img)

  x = np.expand_dims(x, axis = 0)

  images = np.vstack([x])

  classes = model.predict(images, batch_size=10)

  print(fn)

  print(classes)

  if classes[0][0]==1:

    print('Tangan ini menunjukkan BATU')

  elif classes[0][1]==1:

    print('Tangan ini menunjukkan GUNTING')

  elif classes[0][2]==1:

    print('Tangan ini menunjukkan KERTAS')

  else:

    print('TIDAK DIKETAHUI')

Please help

Upvotes: 0

Views: 9716

Answers (3)

Pradyut
Pradyut

Reputation: 123

Because model has not been defined before using it in the line classes = model.predict(images, batch_size=10). First define a model and then use it, for eg;

from sklearn.linear_model import LinearRegression
    
x = 30 * np.random.random((20, 1))
y = 0.5 * x + 1.0 + np.random.normal(size=x.shape)
    
model = LinearRegression()
model.fit(x, y)
    
x_new = np.linspace(0, 30, 100)
y_new = model.predict(x_new[:, np.newaxis])

In this example, first a linear regression model is defined in the line model = LinearRegression() and then that model has been used to predict the new values in line y_new = model.predict(x_new[:, np.newaxis]).

Same thing you need to apply, first define a model that you want to use and then use it to predict the answer. Otherwise if you are using a predefined model from somewhere, you need to import it into your program.

Upvotes: 1

ASFAW AYALKIBET
ASFAW AYALKIBET

Reputation: 54

in your code you have

classes = model.predict(images, batch_size=10)

have you imported model? (or make sure if it exists in you libraries imported)

Upvotes: 0

Joe Thor
Joe Thor

Reputation: 1260

Model is not defined.

You need to instantiate before using it.

Looks like you are using keras.

Here is the documentation for their model API.

https://keras.io/api/models/model/#model-class

Below is an example of instantiating a model from their API docs.

import tensorflow as tf

inputs = tf.keras.Input(shape=(3,))
x = tf.keras.layers.Dense(4, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(5, activation=tf.nn.softmax)(x)
model = tf.keras.Model(inputs=inputs, outputs=outputs)

Upvotes: 0

Related Questions