インポートのNPとしてnumpyの インポートのTFとしてtensorflow インポートPLTとしてmatplotlib.pyplot #随机调整图片的色彩、定义两种顺序。 DEF distort_color(画像、color_ordering = 0) 場合 == color_ordering :0 画像 = tf.image.random_brightnessを(画像、max_delta = 32/255 ) 、画像 = tf.image.random_saturation(画像、低級= 0.5、上側= 1.5 ) 画像 = tf.image.random_hue(画像、max_delta = 0.2 ) 画像 = tf.image.random_contrast(上側= 0.5下方画像、= 1.5 ) 他: 画像= tf.image.random_saturation(画像、低級= 0.5、上側= 1.5 ) 画像 = tf.image.random_brightness(画像、max_delta = 32/255 ) 、画像 = tf.image.random_contrast(画像、0.5 =上下= 1.5 ) 画像 = tf.image.random_hue(画像、max_delta = 0.2 ) 戻り tf.clip_by_value(画像、0.0、1.0)
#画像の前処理は、画像データは、ニューラルネットワークの入力層に変換されます。 DEF (画像、高さ、幅、BBOX)preprocess_for_train: #は、コールアウトボックスかどうかを確認します。 IF BBOXはありませんなし: BBOX = tf.constant([0.0、0.0、1.0、1.0]、DTYPE = tf.float32、形状= 1 ,. 1 ,. 4。]) IF image.dtype =!tf.float32: イメージ = TF .image.convert_image_dtype(画像、DTYPE = tf.float32) #ランダム画像ブロックを取ら。 bbox_begin、bbox_size、_ = tf.image.sample_distorted_bounding_box(tf.shape(イメージ)、bounding_boxes = BBOX、min_object_covered = 0.4 ) bbox_begin、bbox_size、_= tf.image.sample_distorted_bounding_box(tf.shape(画像)、bounding_boxes = BBOX、min_object_covered = 0.4 ) distorted_image = tf.slice(画像、bbox_begin、bbox_size) #将随机截取的图片调整为神经网络输入层的大小。 distorted_image = tf.image.resize_images(distorted_image、[高さ、幅]、方法= np.random.randint(4 )) distorted_image = tf.image.random_flip_left_right(distorted_image) distorted_image = distort_color(distorted_image、np.random.randint(2 ) ) 戻り distorted_image
#读取图片。 image_raw_data = tf.gfile.FastGFile(" F:\\ TensorFlowGoogle \\ 201806-githubの\\データセット\\はcat.jpg "、" RB " ).read() tf.Session有する()などのSES : img_data = tf.image.decode_jpeg(image_raw_data) 箱 = tf.constant([[[0.05、0.05、0.9、0.7]、[0.35、0.47、0.5、0.56 ]]]) のための I における範囲(9 ): 結果 = preprocess_for_train(img_data、299、299 、ボックス) plt.imshow(result.eval()) plt.show()