Note: compared to fbresnet_augmentor, it lacks some photometric augmentation that may have a small effect (0.1~0.2%) on accuracy.
(isTrain)
| 175 | |
| 176 | |
| 177 | def fbresnet_mapper(isTrain): |
| 178 | """ |
| 179 | Note: compared to fbresnet_augmentor, it |
| 180 | lacks some photometric augmentation that may have a small effect (0.1~0.2%) on accuracy. |
| 181 | """ |
| 182 | JPEG_OPT = {'fancy_upscaling': True, 'dct_method': 'INTEGER_ACCURATE'} |
| 183 | |
| 184 | def uint8_resize_bicubic(image, shape): |
| 185 | ret = tf.image.resize_bicubic([image], shape) |
| 186 | return tf.cast(tf.clip_by_value(ret, 0, 255), tf.uint8)[0] |
| 187 | |
| 188 | def resize_shortest_edge(image, image_shape, size): |
| 189 | shape = tf.cast(image_shape, tf.float32) |
| 190 | w_greater = tf.greater(image_shape[0], image_shape[1]) |
| 191 | shape = tf.cond(w_greater, |
| 192 | lambda: tf.cast([shape[0] / shape[1] * size, size], tf.int32), |
| 193 | lambda: tf.cast([size, shape[1] / shape[0] * size], tf.int32)) |
| 194 | |
| 195 | return uint8_resize_bicubic(image, shape) |
| 196 | |
| 197 | def center_crop(image, size): |
| 198 | image_height = tf.shape(image)[0] |
| 199 | image_width = tf.shape(image)[1] |
| 200 | |
| 201 | offset_height = (image_height - size) // 2 |
| 202 | offset_width = (image_width - size) // 2 |
| 203 | image = tf.slice(image, [offset_height, offset_width, 0], [size, size, -1]) |
| 204 | return image |
| 205 | |
| 206 | def lighting(image, std, eigval, eigvec): |
| 207 | v = tf.random_normal(shape=[3], stddev=std) * eigval |
| 208 | inc = tf.matmul(eigvec, tf.reshape(v, [3, 1])) |
| 209 | image = tf.cast(tf.cast(image, tf.float32) + tf.reshape(inc, [3]), image.dtype) |
| 210 | return image |
| 211 | |
| 212 | def validation_mapper(byte): |
| 213 | image = tf.image.decode_jpeg( |
| 214 | tf.reshape(byte, shape=[]), 3, **JPEG_OPT) |
| 215 | image = resize_shortest_edge(image, tf.shape(image), 256) |
| 216 | image = center_crop(image, 224) |
| 217 | image = tf.reverse(image, axis=[2]) # to BGR |
| 218 | return image |
| 219 | |
| 220 | def training_mapper(byte): |
| 221 | jpeg_shape = tf.image.extract_jpeg_shape(byte) # hwc |
| 222 | bbox_begin, bbox_size, distort_bbox = tf.image.sample_distorted_bounding_box( |
| 223 | jpeg_shape, |
| 224 | bounding_boxes=tf.zeros(shape=[0, 0, 4]), |
| 225 | min_object_covered=0, |
| 226 | aspect_ratio_range=[0.75, 1.33], |
| 227 | area_range=[0.08, 1.0], |
| 228 | max_attempts=10, |
| 229 | use_image_if_no_bounding_boxes=True) |
| 230 | |
| 231 | is_bad = tf.reduce_sum(tf.cast(tf.equal(bbox_size, jpeg_shape), tf.int32)) >= 2 |
| 232 | |
| 233 | def good(): |
| 234 | offset_y, offset_x, _ = tf.unstack(bbox_begin) |
no outgoing calls
no test coverage detected
searching dependent graphs…