1

我正在尝试在边缘设备上使用迁移学习构建一个水稻分类器,我在https://github.com/ADLsourceCode/TensorflowJS获得了教程的帮助

我的示例数据位于https://www.dropbox.com/s/esirpr6q1lsdsms/ricetransfer1.zip?dl=0

我使用下面提到的用于大米分类的代码在本地保存了模型,并与 vgg 和 mobilenet 一起保存在文件夹 TensorflowJS/Mobilenet_VGG16_Keras_To_TensorflowJS/static/ 中,但是我无法在浏览器中的 tensorflowjs 上加载大米模型。

如果我尝试在本地系统中保存 vgg 模型并将模型加载到 tensoflowjs(在浏览器中)中,它运行良好。

# Base variables
import os
base_dir = 'ricetransfer1/'
train_dir = os.path.join(base_dir, 'train')
validation_dir = os.path.join(base_dir, 'validation')
test_dir = os.path.join(base_dir, 'test')
train_cats_dir = os.path.join(train_dir, 'KN')
train_dogs_dir = os.path.join(train_dir, 'DM')

train_size, validation_size, test_size = 90, 28, 26
#train_size, validation_size, test_size = 20, 23, 14

img_width, img_height = 224, 224  # Default input size for VGG16

# Instantiate convolutional base
from keras.applications import VGG16
import tensorflowjs as tfjs
import tensorflow as tf
tf.compat.v1.disable_eager_execution()

img_width, img_height = 224, 224  # Default input size for VGG16

conv_base = VGG16(weights='imagenet', 
              include_top=False,
              input_shape=(img_width, img_height, 3))  
# 3 = number of channels in RGB pictures

 #saving the vgg model to run it locally
 tfjs.converters.save_keras_model(conv_base, '/TensorflowJS/Mobilenet_VGG16_Keras_To_TensorflowJS/static/vgg')

# Check architecture
conv_base.summary()


# Extract features
import os, shutil
from keras.preprocessing.image import ImageDataGenerator
import numpy as np
train_size, validation_size, test_size = 90, 28, 25

datagen = ImageDataGenerator(rescale=1./255)
batch_size = 1
#train_dir = "ricetransfer1/train"
#validation_dir = "ricetransfer1/validation"
#test_dir="ricetransfer1/test"
#indices = np.random.choice(range(len(X_train)))

def extract_features(directory, sample_count):
#sample_count= X_train.ravel()

features = np.zeros(shape=(sample_count, 7, 7, 512))  # Must be equal to the output of the convolutional base
labels = np.zeros(shape=(sample_count))
# Preprocess data
generator = datagen.flow_from_directory(directory,
                                        target_size=(img_width,img_height),
                                        batch_size = batch_size,
                                        class_mode='binary')
# Pass data through convolutional base
i = 0
for inputs_batch, labels_batch in generator:
    features_batch = conv_base.predict(inputs_batch)
    features[i * batch_size: (i + 1) * batch_size] = features_batch
    labels[i * batch_size: (i + 1) * batch_size] = labels_batch
    i += 1
    if i * batch_size >= sample_count:
        break
return features, labels

train_features, train_labels = extract_features(train_dir, train_size)  # Agree with our small dataset size
validation_features, validation_labels = extract_features(validation_dir, validation_size)
 test_features, test_labels = extract_features(test_dir, test_size)



# Define model
from keras import models
from keras import layers
from keras import optimizers

epochs = 2

ricemodel = models.Sequential()
ricemodel.add(layers.Flatten(input_shape=(7,7,512)))
ricemodel.add(layers.Dense(256, activation='relu', input_dim=(7*7*512)))
ricemodel.add(layers.Dropout(0.5))
ricemodel.add(layers.Dense(1, activation='sigmoid'))
ricemodel.summary()

 # Compile model
 ricemodel.compile(optimizer=optimizers.Adam(),
          loss='binary_crossentropy',
          metrics=['acc'])


# Train model
import os
history = ricemodel.fit(train_features, train_labels,
                epochs=epochs,
                batch_size=batch_size, 
                validation_data=(validation_features, validation_labels))


##saving the rice classification model to run it locally
 tfjs.converters.save_keras_model(ricemodel, '/TensorflowJS/Mobilenet_VGG16_Keras_To_TensorflowJS/static/rice/')

我想,大米模型有一些错误,我该如何解决这个问题?

预期的输出是使用 tensorflowjs 在浏览器上运行大米分类

4

1 回答 1

1

我认为这里可能是由于 tfjs 文件的旧版本而导致您遇到错误。

将最新版本更新为

<script  src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.5"></script> 

在您的 html 页面中,但由于图像大小不同,可能会出现新错误。

我会建议在浏览器中打开开发模式以查看确切的错误,这样它就起作用了。

于 2019-08-29T13:06:29.313 回答