我正在使用TFOD创建一个对象检测模型。我必须在我的image.What中检测一个非常小的对象,这应该是输入到我的模型中的图像的理想大小吗?我的一些图像非常大。
另外,我已经给一些图片贴上了标签。我也可以调整这些图像的大小吗?还是我得再给他们贴上标签?
发布于 2022-05-11 08:16:17
当你做图像识别,你不期望他们有100%的正确,类似于输入图像或VDO输入,但对齐或后定位。
这不是最后的结果,我这样做只是为了好玩,但你可以看到,100%类似的培训输入与小数据集是不提供准确性的魅力图片。
样本
import os
from os.path import exists
import tensorflow as tf
import cv2
import matplotlib.pyplot as plt
import matplotlib.animation as animation
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
[PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
None
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)
print(physical_devices)
print(config)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Variables
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
path_1 = "F:\\datasets\\downloads\\Duck_Shoe_2\\Duck\\"
list_picture = []
list_actual_picture = []
list_label = []
list_actual_label = ['None', 'Duck']
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Initialize
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model = tf.keras.models.Sequential([
tf.keras.layers.InputLayer(input_shape=( 29, 39, 3 )),
tf.keras.layers.Conv2D(32, (3, 3), activation='relu'),
tf.keras.layers.MaxPooling2D((2, 2)),
tf.keras.layers.Reshape((234, 32)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32, return_sequences=True, return_state=False)),
tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(32)),
])
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64))
model.add(tf.keras.layers.Dense(2))
model.summary()
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: DataSet
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
for file in os.listdir( path_1 ):
image = plt.imread( path_1 + file )
list_actual_picture.append( image )
image = tf.image.resize(image, [29, 39]).numpy()
list_picture.append( image )
list_label.append( 1 )
list_picture = tf.cast( list_picture, dtype=tf.int64 )
list_picture = tf.constant( list_picture, shape=( 6, 1, 29, 39, 3 ) )
list_label = tf.cast( list_label, dtype=tf.int64 )
list_label = tf.constant( list_label, shape=( 6, 1, 1, 1 ) )
dataset = tf.data.Dataset.from_tensor_slices(( list_picture, list_label ))
list_picture = tf.constant( list_picture, shape=( 6, 29, 39, 3 ) )
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Optimizer
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
optimizer = tf.keras.optimizers.Nadam( learning_rate=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-07, name='Nadam' )
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Loss Fn
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
lossfn = tf.keras.losses.MeanSquaredLogarithmicError(reduction=tf.keras.losses.Reduction.AUTO, name='mean_squared_logarithmic_error')
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Model Summary
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
model.compile(optimizer=optimizer, loss=lossfn, metrics=['accuracy'])
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
: Training
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
history = model.fit(dataset, epochs=3 ,validation_data=(dataset))
plt.figure(figsize=(3,2))
plt.title("Actors recognitions")
for i in range(len(list_picture)):
img = tf.keras.preprocessing.image.array_to_img(
list_picture[i],
data_format=None,
scale=True
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0)
predictions = model.predict(img_array)
score = tf.nn.softmax(predictions[0])
plt.subplot(3, 2, i + 1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(list_actual_picture[i])
plt.xlabel(str(round(score[tf.math.argmax(score).numpy()].numpy(), 2)) + ":" + str(list_actual_label[tf.math.argmax(score)]))
plt.show()
input('...')
输出
https://stackoverflow.com/questions/72195548
复制相似问题