Tensorflow.js隐式形状不能是小数。得到2500/7500

2024-10-01 22:39:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用python训练模型,然后将模型加载到Tensorflow.js中

for index, name in enumerate(folder):
    dir = "./cnn/" + name
    files = glob.glob(dir + "/*.jpg")
    for i, file in enumerate(files):
        image = Image.open(file)
        image = image.convert("RGB")
        image = image.resize((image_size, image_size))
        data = np.asarray(image)
        X.append(data)
        Y.append(index)

X = np.array(X)
Y = np.array(Y)
X = X.astype('float32')
X = X / 255.0

Y = np_utils.to_categorical(Y, dense_size)
X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.10)

model = Sequential()
model.add(Conv2D(32, (3, 3), padding='same',input_shape=X_train.shape[1:]))
model.add(Activation('relu'))
model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu'))
model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Flatten())
model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(dense_size))
model.add(Activation('softmax'))

model.summary()
optimizers ="Adadelta"
results = {}

epochs = 200
model.compile(loss='categorical_crossentropy', optimizer=optimizers, metrics=['accuracy'])
results[0]= model.fit(X_train, y_train, validation_split=0.2, epochs=epochs)

model_json_str = model.to_json()
open('mnist_mlp_model.json', 'w').write(model_json_str)
model.save_weights('mnist_mlp_weights.h5')
tfjs.converters.save_keras_model(model, "js_model")

Tensorflow.js

  /* Get image and pre process */
  const t0 = performance.now();
  const inputTensor = await getImage(cam);
  
  /* Inference */
  const t1 = performance.now();
  const scores = await model.predict(inputTensor).data();
  inputTensor.dispose();

  /* Post process */
  const t2 = performance.now();
  const maxScoreIndex = await tf.argMax(scores).array();

  /* Display result */
  const t3 = performance.now();
  //console.log(scores);
  document.getElementById("result").innerHTML = "Num: " + maxScoreIndex + " (" + scores[maxScoreIndex].toFixed(3) + ")";

  const t4 = performance.now();
  document.getElementById("time").innerHTML = `Time[ms]: Total = ${(t4 - t0).toFixed(3)},
  PreProcess = ${(t1 - t0).toFixed(3)},
  Inference = ${(t2 - t1).toFixed(3)},
  PostProcess = ${(t3 - t2).toFixed(3)}`;

但是,我得到了以下错误

 Error: Error when checking : expected conv2d_input to have shape [null,50,50,3] but got array with shape [1,50,50,1].

因此,我尝试使用上面的代码来重塑图像,请参考下面的URL Uncaught Error: Error when checking : expected conv2d_input to have 4 dimension(s), but got array with shape [275,183,3]

inputTensor.reshape([-1, 50, 50, 3]);

但是,我得到了以下错误

Error: The implicit shape can't be a fractional number. Got 2500 / 7500

我是否必须改变Keras的训练方法并再次训练

2021/03/29 X_列车的形状。形状[1:]和X_列车

打印(X_列形状[1:])

(50, 50, 3)

打印(X_系列)

[[[[0.67058825 0.6        0.47058824]
   [0.6784314  0.6039216  0.47843137]
   [0.67058825 0.59607846 0.4745098 ]
   ...
   [0.6745098  0.5372549  0.36078432]
   [0.6431373  0.5019608  0.3137255 ]
   [0.6313726  0.4862745  0.29803923]]

  [[0.6862745  0.6117647  0.4862745 ]
   [0.68235296 0.6039216  0.48235294]
   [0.6784314  0.6039216  0.4862745 ]
   ...
   [0.63529414 0.49019608 0.30588236]
   [0.62352943 0.48235294 0.2901961 ]
   [0.61960787 0.4745098  0.2901961 ]]

  [[0.68235296 0.6117647  0.49019608]
   [0.68235296 0.60784316 0.49019608]
   [0.6745098  0.6039216  0.4862745 ]
   ...
   [0.627451   0.4862745  0.2901961 ]
   [0.62352943 0.47843137 0.28235295]
   [0.61960787 0.47058824 0.28235295]]

  ...

  [[0.8627451  0.827451   0.78431374]
   [0.85882354 0.81960785 0.7764706 ]
   [0.8666667  0.83137256 0.8       ]
   ...
   [0.80784315 0.74509805 0.73333335]
   [0.8156863  0.7411765  0.7372549 ]
   [0.8117647  0.7411765  0.73333335]]

  [[0.7647059  0.7254902  0.6862745 ]
   [0.8666667  0.83137256 0.7882353 ]
   [0.85882354 0.8235294  0.78431374]
   ...
   [0.80784315 0.7372549  0.7294118 ]
   [0.80784315 0.7372549  0.73333335]
   [0.80784315 0.73333335 0.7294118 ]]

  [[0.5647059  0.52156866 0.47843137]
   [0.84313726 0.80784315 0.7647059 ]
   [0.8627451  0.827451   0.7882353 ]
   ...
   [0.8039216  0.73333335 0.7254902 ]
   [0.8039216  0.73333335 0.7254902 ]
   [0.8039216  0.73333335 0.7176471 ]]]


 [[[0.6745098  0.6039216  0.47843137]
   [0.6784314  0.6039216  0.47843137]
   [0.6745098  0.6        0.4745098 ]
   ...
   [0.6666667  0.5294118  0.34509805]
   [0.6392157  0.49803922 0.30588236]
   [0.6313726  0.48235294 0.29411766]]

  [[0.6862745  0.6117647  0.49411765]
   [0.6784314  0.60784316 0.4862745 ]
   [0.6745098  0.60784316 0.4862745 ]
   ...
   [0.63529414 0.49019608 0.29803923]
   [0.62352943 0.48235294 0.28627452]
   [0.62352943 0.4745098  0.28235295]]

  [[0.68235296 0.6156863  0.49411765]
   [0.6745098  0.6117647  0.48235294]
   [0.67058825 0.60784316 0.4862745 ]
   ...
   [0.627451   0.4862745  0.2901961 ]
   [0.61960787 0.47843137 0.2901961 ]
   [0.62352943 0.4745098  0.2901961 ]]

  ...

  [[0.85882354 0.8235294  0.78431374]
   [0.85490197 0.8235294  0.7764706 ]
   [0.8666667  0.83137256 0.8       ]
   ...
   [0.80784315 0.74509805 0.73333335]
   [0.8117647  0.7411765  0.73333335]
   [0.8117647  0.7411765  0.73333335]]

  [[0.8039216  0.7647059  0.7294118 ]
   [0.85882354 0.827451   0.78431374]
   [0.85882354 0.8235294  0.78039217]
   ...
   [0.80784315 0.7372549  0.7294118 ]
   [0.80784315 0.7372549  0.7254902 ]
   [0.80784315 0.7372549  0.7254902 ]]

  [[0.60784316 0.5686275  0.5294118 ]
   [0.85490197 0.8235294  0.78039217]
   [0.85490197 0.8235294  0.78039217]
   ...
   [0.8039216  0.73333335 0.72156864]
   [0.8039216  0.73333335 0.7176471 ]
   [0.8039216  0.73333335 0.7176471 ]]]


 [[[0.6745098  0.60784316 0.48235294]
   [0.67058825 0.6039216  0.4862745 ]
   [0.6666667  0.59607846 0.47843137]
   ...
   [0.654902   0.5137255  0.3372549 ]
   [0.6431373  0.49803922 0.30980393]
   [0.6313726  0.48235294 0.29803923]]

  [[0.6745098  0.6039216  0.4862745 ]
   [0.6745098  0.60784316 0.49019608]
   [0.67058825 0.6        0.48235294]
   ...
   [0.6431373  0.49411765 0.3137255 ]
   [0.63529414 0.4862745  0.3019608 ]
   [0.6313726  0.47843137 0.29803923]]

  [[0.6784314  0.6117647  0.49803922]
   [0.6745098  0.60784316 0.49019608]
   [0.67058825 0.6039216  0.4862745 ]
   ...
   [0.6392157  0.49411765 0.3137255 ]
   [0.6392157  0.49019608 0.30588236]
   [0.6313726  0.48235294 0.29803923]]

  ...

  [[0.54509807 0.5019608  0.46666667]
   [0.8352941  0.8        0.7607843 ]
   [0.85490197 0.81960785 0.78039217]
   ...
   [0.8235294  0.7529412  0.7529412 ]
   [0.8235294  0.7529412  0.7490196 ]
   [0.8235294  0.7529412  0.74509805]]

  [[0.46666667 0.42745098 0.3882353 ]
   [0.6862745  0.64705884 0.60784316]
   [0.8666667  0.8352941  0.7921569 ]
   ...
   [0.81960785 0.7490196  0.7411765 ]
   [0.8156863  0.74509805 0.7372549 ]
   [0.81960785 0.7490196  0.74509805]]

  [[0.5137255  0.47058824 0.41960785]
   [0.5254902  0.47843137 0.43529412]
   [0.827451   0.7921569  0.7529412 ]
   ...
   [0.8156863  0.74509805 0.73333335]
   [0.8156863  0.74509805 0.73333335]
   [0.8156863  0.74509805 0.7372549 ]]]


 ...


 [[[0.8352941  0.76862746 0.6901961 ]
   [0.8392157  0.76862746 0.6901961 ]
   [0.84705883 0.7764706  0.69803923]
   ...
   [0.85882354 0.7882353  0.7019608 ]
   [0.85882354 0.7882353  0.7019608 ]
   [0.8627451  0.7921569  0.7058824 ]]

  [[0.827451   0.7647059  0.6862745 ]
   [0.827451   0.76862746 0.6901961 ]
   [0.83137256 0.77254903 0.69411767]
   ...
   [0.85882354 0.7882353  0.7019608 ]
   [0.85490197 0.78431374 0.69803923]
   [0.85490197 0.78431374 0.69803923]]

  [[0.8235294  0.75686276 0.68235296]
   [0.81960785 0.7607843  0.6862745 ]
   [0.8235294  0.7647059  0.6901961 ]
   ...
   [0.85490197 0.78431374 0.69803923]
   [0.84705883 0.7764706  0.69411767]
   [0.8509804  0.78039217 0.69411767]]

  ...

  [[0.36862746 0.27450982 0.25490198]
   [0.36862746 0.2784314  0.25490198]
   [0.36862746 0.28627452 0.2509804 ]
   ...
   [0.14901961 0.16470589 0.15294118]
   [0.14901961 0.16862746 0.16078432]
   [0.15294118 0.17254902 0.16862746]]

  [[0.4        0.29803923 0.28235295]
   [0.39607844 0.29803923 0.27450982]
   [0.38039216 0.29411766 0.2627451 ]
   ...
   [0.15294118 0.19607843 0.1882353 ]
   [0.14901961 0.19607843 0.19607843]
   [0.15294118 0.20392157 0.20392157]]

  [[0.29411766 0.21568628 0.20392157]
   [0.28627452 0.20392157 0.19215687]
   [0.27058825 0.19215687 0.18039216]
   ...
   [0.16862746 0.21568628 0.22352941]
   [0.15686275 0.21568628 0.22352941]
   [0.15686275 0.21568628 0.22745098]]]


 [[[0.65882355 0.5921569  0.4745098 ]
   [0.65882355 0.5882353  0.4745098 ]
   [0.65882355 0.59607846 0.4745098 ]
   ...
   [0.62352943 0.48235294 0.29411766]
   [0.61960787 0.47843137 0.2901961 ]
   [0.61960787 0.47843137 0.2901961 ]]

  [[0.6666667  0.6        0.49019608]
   [0.67058825 0.6039216  0.49411765]
   [0.67058825 0.60784316 0.49411765]
   ...
   [0.6313726  0.4862745  0.3019608 ]
   [0.627451   0.47843137 0.29411766]
   [0.627451   0.47843137 0.29411766]]

  [[0.6745098  0.6117647  0.5019608 ]
   [0.6784314  0.61960787 0.5137255 ]
   [0.6745098  0.61960787 0.50980395]
   ...
   [0.6392157  0.49019608 0.30588236]
   [0.6431373  0.49411765 0.30980393]
   [0.64705884 0.49803922 0.32156864]]

  ...

  [[0.4862745  0.4509804  0.40392157]
   [0.59607846 0.5568628  0.5176471 ]
   [0.8666667  0.827451   0.7882353 ]
   ...
   [0.8117647  0.74509805 0.7372549 ]
   [0.8156863  0.7411765  0.7411765 ]
   [0.8156863  0.74509805 0.7372549 ]]

  [[0.59607846 0.5568628  0.5137255 ]
   [0.47843137 0.4392157  0.39215687]
   [0.7176471  0.68235296 0.6431373 ]
   ...
   [0.80784315 0.74509805 0.73333335]
   [0.80784315 0.7411765  0.7372549 ]
   [0.80784315 0.7372549  0.7294118 ]]

  [[0.78431374 0.7490196  0.70980394]
   [0.53333336 0.49019608 0.44705883]
   [0.5176471  0.47843137 0.4392157 ]
   ...
   [0.80784315 0.7372549  0.7294118 ]
   [0.8        0.7411765  0.7294118 ]
   [0.80784315 0.7372549  0.7294118 ]]]


 [[[0.6666667  0.6        0.4745098 ]
   [0.6627451  0.6        0.47058824]
   [0.6666667  0.6039216  0.47843137]
   ...
   [0.7019608  0.5764706  0.40784314]
   [0.6745098  0.5411765  0.3647059 ]
   [0.6431373  0.5058824  0.32941177]]

  [[0.67058825 0.60784316 0.48235294]
   [0.6745098  0.60784316 0.49411765]
   [0.67058825 0.6039216  0.49019608]
   ...
   [0.654902   0.5176471  0.34509805]
   [0.627451   0.49411765 0.30588236]
   [0.61960787 0.47843137 0.2901961 ]]

  [[0.6745098  0.60784316 0.4862745 ]
   [0.6745098  0.60784316 0.49019608]
   [0.6666667  0.6        0.49019608]
   ...
   [0.6313726  0.49411765 0.30980393]
   [0.62352943 0.4862745  0.29803923]
   [0.6156863  0.47843137 0.2901961 ]]

  ...

  [[0.85490197 0.8235294  0.78431374]
   [0.8627451  0.827451   0.8       ]
   [0.83137256 0.79607844 0.80784315]
   ...
   [0.8117647  0.7529412  0.7529412 ]
   [0.80784315 0.7490196  0.7411765 ]
   [0.80784315 0.7490196  0.7411765 ]]

  [[0.85490197 0.827451   0.7882353 ]
   [0.85882354 0.827451   0.7882353 ]
   [0.8627451  0.83137256 0.81960785]
   ...
   [0.8117647  0.7490196  0.7490196 ]
   [0.80784315 0.7490196  0.7372549 ]
   [0.80784315 0.7490196  0.7372549 ]]

  [[0.85882354 0.827451   0.78431374]
   [0.85882354 0.827451   0.78431374]
   [0.8666667  0.8392157  0.8039216 ]
   ...
   [0.80784315 0.74509805 0.74509805]
   [0.80784315 0.74509805 0.74509805]
   [0.80784315 0.74509805 0.74509805]]]]

Tags: imageaddsizemodelperformancenptrainerror

热门问题