python模型预测_python – 在从保存的模型进行预测时,测试数据预测会产生随机结果...

我正在使用Keras和TensorFlow对通过平铺成256×256图块的航拍图像进行分类.该模型将训练数据(即构成研究区域的256×256图像区块)分成70%的训练数据和30%的验证数据.使用顺序模型,然后使用图像数据生成器.最后,使用拟合生成器将模型拟合到数据.然后将模型保存为h5格式,以用于预测具有不同研究区域中的其他图像的类.

当我使用70%/ 30%训练/验证分割运行模型时,验证图像的预测效果很好,精度越来越高,每个时期的损失也在逐渐减少.另外,当我通过将概率阵列连接到表示图块边界的矢量多边形来可视化预测(即概率阵列)时,分类结果看起来非常好.

我的问题是,当我使用保存的h5模型对新图像进行预测时 – 结果是无意义的,并且对于每个图块看起来是随机的.就像概率数组被随机混洗一样,当我将结果连接到矢量图像边界图块时,结果看起来完全是随机的.我该如何解决这个问题?

以下是用于训练模型的代码的相关部分:

base_model = applications.VGG16(weights='imagenet', include_top=False, input_shape=(img_rows, img_cols, img_channel))

add_model = Sequential()

add_model.add(Flatten(input_shape=base_model.output_shape[1:]))

add_model.add(Dense(256, activation='relu'))

add_model.add(Dense(n_classes, activation='sigmoid')) # n classes

model = Model(inputs=base_model.input, outputs=add_model(base_model.output))

model.compile(loss='binary_crossentropy', optimizer=optimizers.SGD(lr=1e-4, momentum=0.9),

metrics=['accuracy'])

######################

batch_size = 32

epochs = 50

print('Running the image data generator...')

train_datagen = ImageDataGenerator(

rotation_range=30,

width_shift_range=0.1,

height_shift_range=0.1,

horizontal_flip=True)

train_datagen.fit(x_train)

print('Fitting the model...')

history = model.fit_generator(

train_datagen.flow(x_train, y_train, batch_size=batch_size),

steps_per_epoch=x_train.shape[0] // batch_size,

epochs=epochs,

#validation_data=(x_valid, y_valid),

#callbacks=[ModelCheckpoint(model_checkpoint, monitor='val_acc', save_best_only=True)]

)

######################

## Predict

#print('Predicting...')

#p_valid = model.predict(x_valid, batch_size=128)

## Write predictions to csv

#print('Saving predictions to CSV...')

#df = pd.DataFrame(p_valid)

#df['image'] = split + 1 + df.index

#df.to_csv(out_csv, index=False, header=False)

"""

Save model, including these details:

-the architecture of the model, allowing to re-create the model

-the weights of the model

-the training configuration (loss, optimizer)

-the state of the optimizer, allowing to resume training exactly where you left off.

"""

print("Saving model")

model.save("/vgg16-model-50epochs.h5")

print('Processing complete.')

以下脚本使用上面保存的模型对来自不同研究区域的测试图像进​​行预测.注意,在上面的最终训练中没有70/30训练/验证分割 – 我只是使用100%的瓷砖来训练模型,然后我在以下脚本中保存和重用:

import glob, os, time

import cv2

import numpy as np

import pandas as pd

from keras.models import load_model

#from keras.models import model_from_json

# Path to the input tiles which will be used to predict classes

inws = '/image-directory-for-another-study-area'

tiles = glob.glob(os.path.join(inws, '*.tif'))

# h5 file from trained model

in_h5 = "/vgg16-model-50epochs.h5"

# Output model predictions in csv format

out_csv = '/new-predictions.csv'

# Read images and convert to numpy array

x_test = np.array([cv2.imread(tile) for tile in tiles], np.float16) / 255.

print('Loading existing model...')

loaded_model = load_model(in_h5)

print("Predicting on image tiles...")

predictions = loaded_model.predict(x_test, batch_size=128)

# Save to csv

df = pd.DataFrame(predictions)

df['image'] = df.index + 1

df.to_csv(out_csv, index=False, header=False)

print("Predictions saved to disk: {0}".format(out_csv))

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值