Then after the prediction is finished, the gray placeholders is removed. In order not to cause inaccurate prediction results due to the scale problem, the author adds **gray placeholders for the place where the image is not proportional** when resize, as shown in the following figure. It should be noted here that the predicted graph is not necessarily the same scale as INPUT_SHAPE. In the prediction model part, first, resize the file to the INPUT_SHAPE size of the model. save_weights ( filepath, overwrite = True ) else : self. verbose > 0 : print ( ' \n Epoch d: saving model to % s' % ( epoch 1, filepath )) if self. verbose > 0 : print ( ' \n Epoch d: % s did not improve' % ( epoch 1, self. save ( filepath, overwrite = True ) else : if self. verbose > 0 : print ( ' \n Epoch d: % s improved from %0.5 f to %0.5 f,' ' saving model to % s' % ( epoch 1, self. monitor ), RuntimeWarning ) else : if self. warn ( 'Can save best model only with % s available, ' 'skipping.' % ( self. format ( epoch = epoch 1, ** logs ) if self. epochs_since_last_save = 0 filepath = self. In this way, the user can select a better model after training.ĭef on_epoch_end ( self, epoch, logs = None ): clear_output ( wait = True ) show_predictions () print ( ' \n Sample Prediction after epoch self. ModelCheckpointCallback is used to save the weights (model) in the logs folder after training an epoch, and record the accuracy and loss rate after each epoch. In the Callback part, the author mainly uses Displa圜allback and ModelCheckpointCallback.ĭispla圜allback is used to display the predicted results after training an epoch, so as to observe the effect of the model. Conv2DTranspose ( filters = output_channels, kernel_size = 3, strides = 2, padding = 'same' ) #64圆4 -> 128x128 Concatenate () x = concat () # This is the last layer of the model Skips = down_stack ( inputs ) x = skips skips = reversed ( skips ) # Upsampling and establishing the skip connectionsįor up, skip in zip ( up_stack, skips ): x = up ( x ) concat = tf. Input ( shape = INPUT_SHAPE ) # Downsampling through the model Here, the author refers to, you can check the detailed analysis.ĭef unet_model ( output_channels : int ): inputs = tf. The model definition part is relatively simple, as in the paper, mainly downsampling, upsampling, and concat. Image, _, _ = resize_image ( image, ( w, h )) label, _, _ = resize_label ( label, ( w, h )) if random : # flipįlip = self. array ( label )) h, w, _ = input_shape # resize rand () * ( b - a ) a def process_data ( self, image, label, input_shape, random = True ): image = cvtColor ( image ) label = Image. array ( targets ) return images, targets def rand ( self, a = 0, b = 1 ): return np. dataset_path, "SegmentationClassPNG" ), name ".png" )) jpg, png = self. dataset_path, "JPEGImages" ), name ".jpg" )) png = Image. batch_size )) def _getitem_ ( self, index ): images = targets = for i in range ( index * self. dataset_path = dataset_path def _len_ ( self ): return math. annotation_lines = annotation_lines self. Sequence ): def _init_ ( self, annotation_lines, input_shape, batch_size, num_classes, train, dataset_path ): self. Because the model has a fixed input shape, the resize operation is performed in the process_data method during the training process, data enhancement can also be added, and a simple flip is used here.Ĭlass UnetDataset ( tf. Return a set of batch_size data through the _getitem_ method, which includes the original image (images) and the label image (targets). readlines () train_batches = UnetDataset ( train_lines, INPUT_SHAPE, BATCH_SIZE, NUM_CLASSES, True, dataset_path ) val_batches = UnetDataset ( val_lines, INPUT_SHAPE, BATCH_SIZE, NUM_CLASSES, False, dataset_path ) STEPS_PER_EPOCH = len ( train_lines ) // BATCH_SIZE VALIDATION_STEPS = len ( val_lines ) // BATCH_SIZE // VAL_SUBSPLITS join ( dataset_path, "ImageSets/Segmentation/val.txt" ), "r", encoding = "utf8" ) as f : val_lines = f. join ( dataset_path, "ImageSets/Segmentation/train.txt" ), "r", encoding = "utf8" ) as f : train_lines = f. Then, it is constructed as a tf. object through the UnetDataset class, which is convenient for direct training through model.fit later.ĭataset_path = 'datasets/train_voc' # read dataset txt files The dataset is divided into the training set and validation set, and files are read from ImageSets/Segmentation/train.txt and ImageSets/Segmentation/val.txt respectively. Open unet.ipynb and select the Python interpreter as unet to start training. The generated datasets/train_voc is the dataset used for training. │ │ └── val.txt # list of validation set image names │ │ ├── train.txt # List of training set image names
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |