(model, train_batch_reader, adam, epoch, batch_num, args)
| 122 | |
| 123 | |
| 124 | def train(model, train_batch_reader, adam, epoch, batch_num, args): |
| 125 | loss_type = args.loss |
| 126 | model.train() |
| 127 | for batch_id, data in enumerate(train_batch_reader()): |
| 128 | start = time.time() |
| 129 | im1_data = np.array( |
| 130 | [x[0] for x in data]).astype('float32') |
| 131 | im2_data = np.array( |
| 132 | [x[1] for x in data]).astype('float32') |
| 133 | flo_data = np.array( |
| 134 | [x[2] for x in data]).astype('float32') |
| 135 | im_all = np.concatenate((im1_data, im2_data), axis=3).astype(np.float32) |
| 136 | im_all = im_all / 255.0 |
| 137 | im_all = np.swapaxes(np.swapaxes(im_all, 1, 2), 1, 3) |
| 138 | label = flo_data / 20.0 |
| 139 | label = np.swapaxes(np.swapaxes(label, 1, 2), 1, 3) |
| 140 | if batch_id % 10 == 0: |
| 141 | im1 = im_all[0, :3, :, :] * 255 |
| 142 | im2 = im_all[0, 3:, :, :] * 255 |
| 143 | im1 = np.swapaxes(np.swapaxes(im1, 0, 1), 1, 2).astype(np.uint8) |
| 144 | im2 = np.swapaxes(np.swapaxes(im2, 0, 1), 1, 2).astype(np.uint8) |
| 145 | |
| 146 | flo = label[0, :, :, :] * 20 |
| 147 | flo = np.swapaxes(np.swapaxes(flo, 0, 1), 1, 2) |
| 148 | imsave('./img1.png', im1) |
| 149 | imsave('./img2.png', im2) |
| 150 | flow_color = flow_vis.flow_to_color(flo, convert_to_bgr=False) |
| 151 | imsave('./hsv_pd.png', flow_color) |
| 152 | H = im_all[0].shape[1] |
| 153 | W = im_all[0].shape[2] |
| 154 | |
| 155 | im_all = fluid.dygraph.to_variable(im_all) |
| 156 | label = fluid.dygraph.to_variable(label) |
| 157 | im_all, [intPaddingLeft, intPaddingRight, intPaddingTop, intPaddingBottom, intWidth, intHeight] = pad_input( |
| 158 | im_all) |
| 159 | |
| 160 | label, _ = pad_input(label) |
| 161 | end = time.time() |
| 162 | read_data_time = end - start |
| 163 | start = time.time() |
| 164 | network_output = model(im_all, output_more=True) |
| 165 | if batch_id % 10 == 0: |
| 166 | flo = network_output[0][0].numpy() * 20.0 |
| 167 | # scale the flow back to the input size |
| 168 | flo = np.swapaxes(np.swapaxes(flo, 0, 1), 1, 2) |
| 169 | flo = flo[intPaddingTop * 2:intPaddingTop * 2 + intHeight * 2, |
| 170 | intPaddingLeft * 2: intPaddingLeft * 2 + intWidth * 2, :] |
| 171 | |
| 172 | u_ = cv2.resize(flo[:, :, 0], (W, H)) |
| 173 | v_ = cv2.resize(flo[:, :, 1], (W, H)) |
| 174 | flo = np.dstack((u_, v_)) |
| 175 | flow_color = flow_vis.flow_to_color(flo, convert_to_bgr=False) |
| 176 | imsave('./hsv_predict.png', flow_color) |
| 177 | loss = multiscaleEPE(network_output, label, loss_type, weights=None, sparse=False) |
| 178 | |
| 179 | end = time.time() |
| 180 | loss.backward() |
| 181 | if args.use_multi_gpu: |
no test coverage detected