sejongresearch / SituationClassifier

자퇴와휴학사이, 장애물 분류기 (2019)
2 stars 1 forks source link

object detection (feat. Tensorflow API) #8

Open nhk9680 opened 5 years ago

nhk9680 commented 5 years ago

Related Works: object detection (김남훈)


코드

                 


Object detection Tensorflow API

detection boxes 값의 의미

(left, right, top, bottom) = (xmin * im_width, xmax * im_width, ymin * im_height, ymax * im_height)

정규화된 좌표, 즉 상대적인 좌표를 제공합니다.

matplotlib 사용시 loop로 인한 memory leack (overflow) 문제

https://stackoverflow.com/questions/7101404/how-can-i-release-memory-after-creating-matplotlib-figures

.clf(), .close(), import gc; gc.collect() 등을 사용하면 된다고 합니다.


다음 계획 

  Dataset이나 Video를 로드하여 demo를 출력해볼 예정


                   

colab으로 학습해보기


다음 계획 

 

NotfoundError : No such file or directory

Traceback (most recent call last): File "models/research/object_detection/dataset_tools/create_pet_tf_record.py", line 318, in tf.app.run() ... File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/errors_impl.py", line 528, in exit c_api.TF_GetCode(self.status.status)) tensorflow.python.framework.errors_impl.NotFoundError: object_detection/data/pet_label_map.pbtxt; No such file or directory ls: cannot access '*.record': No such file or directory

보다시피 실행했던 기본 경로가 /content 이다 보니, object_detection 폴더를 찾지 못한다.

해결 시도

결과

/content/models/research/object_detection/utils/dataset_util.py:75: FutureWarning: The behavior of this method will change in future versions. Use specific 'len(elem)' or 'elem is not None' test instead. if not xml: WARNING:root:Could not find /content/annotations/xmls/Egyptian_Mau_14.xml, ignoring example. WARNING:root:Could not find /content/annotations/xmls/saint_bernard_15.xml, ignoring example. WARNING:root:Could not find /content/annotations/xmls/Ragdoll_199.xml, ignoring example. WARNING:root:Could not find /content/annotations/xmls/Egyptian_Mau_186.xml, ignoring example. WARNING:root:Could not find /content/annotations/xmls/Bengal_175.xml, ignoring example. WARNING:root:Could not find /content/annotations/xmls/samoyed_10.xml, ignoring example. WARNING:root:Could not find /content/annotations/xmls/Egyptian_Mau_156.xml, ignoring example. WARNING:root:Could not find /content/annotations/xmls/Abyssinian_104.xml, ignoring example. WARNING:root:Could not find /content/annotations/xmls/Bengal_111.xml, ignoring example. ls: cannot access '*.record': No such file or directory

이는 현재 제공자 측에서 파일이 누락된 상태에서 발생하는 warning입니다.

그러나 이는 예제파일일 뿐, custom dataset으로 학습 시에는 문제되지 않습니다.

디렉토리에 결과를 보면

total 1161664 drwxr-xr-x 1 root root 4096 Jun 2 07:02 . drwxr-xr-x 1 root root 4096 Jun 2 06:52 .. drwxr-xr-x 4 501 staff 4096 Jun 30 2012 annotations -rw-r--r-- 1 root root 19173078 Jun 30 2012 annotations.tar.gz drwxr-xr-x 8 root root 4096 Jun 2 06:57 cocoapi drwxr-xr-x 1 root root 4096 May 30 16:09 .config drwxr-xr-x 2 1001 1001 299008 Jun 18 2012 images -rw-r--r-- 1 root root 791918971 Jun 18 2012 images.tar.gz drwxr-xr-x 7 root root 4096 Jun 2 06:57 models lrwxrwxrwx 1 root root 32 Jun 2 07:02 object_detection -> models/research/object_detection -rw-r--r-- 1 root root 25979841 Jun 2 07:18 pet_faces_train.record-00000-of-00010 -rw-r--r-- 1 root root 27602249 Jun 2 07:18 pet_faces_train.record-00001-of-00010 -rw-r--r-- 1 root root 25452528 Jun 2 07:18 pet_faces_train.record-00002-of-00010 -rw-r--r-- 1 root root 26200433 Jun 2 07:18 pet_faces_train.record-00003-of-00010 -rw-r--r-- 1 root root 26588912 Jun 2 07:18 pet_faces_train.record-00004-of-00010 -rw-r--r-- 1 root root 27307307 Jun 2 07:18 pet_faces_train.record-00005-of-00010 -rw-r--r-- 1 root root 25732901 Jun 2 07:18 pet_faces_train.record-00006-of-00010 -rw-r--r-- 1 root root 26230339 Jun 2 07:18 pet_faces_train.record-00007-of-00010 -rw-r--r-- 1 root root 26675882 Jun 2 07:18 pet_faces_train.record-00008-of-00010 -rw-r--r-- 1 root root 27137568 Jun 2 07:18 pet_faces_train.record-00009-of-00010 -rw-r--r-- 1 root root 10905831 Jun 2 07:18 pet_faces_val.record-00000-of-00010 -rw-r--r-- 1 root root 11969014 Jun 2 07:18 pet_faces_val.record-00001-of-00010 -rw-r--r-- 1 root root 12041018 Jun 2 07:18 pet_faces_val.record-00002-of-00010 -rw-r--r-- 1 root root 11893796 Jun 2 07:18 pet_faces_val.record-00003-of-00010 -rw-r--r-- 1 root root 10199921 Jun 2 07:18 pet_faces_val.record-00004-of-00010 -rw-r--r-- 1 root root 11576302 Jun 2 07:18 pet_faces_val.record-00005-of-00010 -rw-r--r-- 1 root root 10374480 Jun 2 07:18 pet_faces_val.record-00006-of-00010 -rw-r--r-- 1 root root 11018374 Jun 2 07:18 pet_faces_val.record-00007-of-00010 -rw-r--r-- 1 root root 12441781 Jun 2 07:18 pet_faces_val.record-00008-of-00010 -rw-r--r-- 1 root root 10754658 Jun 2 07:18 pet_faces_val.record-00009-of-00010 drwxr-xr-x 1 root root 4096 May 24 16:08 sample_data

반복적으로 생성된 파일들이 tfrecord임을 추정할 수 있지만, !ls *.record를 실행하면

ls: cannot access '*.record': No such file or directory

라고 에러를 뱉어냅니다.

!ls pet_faces_train.record-00000-of-00010.record 해도 결과는

ls: cannot access 'pet_faces_train.record-00000-of-00010.record': No such file or directory

마찬가지입니다.

unizard commented 5 years ago

@nhk9680 결론이 제공자측의 잘못으로 학습할수 없다 라는게 결론인가요?

nhk9680 commented 5 years ago

@unizard 아니요, 누락된 파일은 Warning으로만 뜨는 걸 보면 추후 진행에는 문제가 없는 듯 합니다. 아직 진행중이라 결론은 정리해서 작성할 예정입니다.

nhk9680 commented 5 years ago

colab에서 cv2.VideoCapture.read() 시 R Channel 소실 문제

whre_is_red

우선 drive에 업로드 된 영상에 문제가 있는지 재생했지만 정상적으로 나옵니다.

colab에서만 이러한 증상이 생기는 건지 로컬에서도 뽑아봤지만, 정상적으로 나옵니다.

imshow()에 문제가 있었다면 다운로드한 이미지는 정상적으로 나와야하지만 그렇지 않습니다.

하나만 그런것도 아니고 모든 프레임이 저렇게 나와서 구글링 중입니다.

unizard commented 5 years ago

@nhk9680 R 채널이 소실되었다는게 어떤의미죠? 지금 콘이 주황색이 아니여서 R채널 소실이라고 보는거죠? 저거 RGB 채널이 BGR로 저장되어 있어서 그래요. 뒤집어서 출력해보세요.

nhk9680 commented 5 years ago

@nhk9680 R 채널이 소실되었다는게 어떤의미죠? 지금 콘이 주황색이 아니여서 R채널 소실이라고 보는거죠? 저거 RGB 채널이 BGR로 저장되어 있어서 그래요. 뒤집어서 출력해보세요.

감사합니다! 원래대로 잘 뽑힙니다.

image=cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

같은 코드, 같은 소스인데 colab에서만 반전되는게 신기하네요..

unizard commented 5 years ago

@nhk9680 코랩에서만 반전이 된다는건 좀 이상한게... 저 현상은 opencv를 사용하면 항상생기는 현상이예요. Opencv의 칼라영상 로더의 채널저장법이 기타 라이브러리들과 차이가 있어요. 아마 로컬머신에서 저런 문제가 발생하지 않은것 처럼 보였다면 분명 시각화하는 부분이 opencv가 아닌 matlabplot 과 같이 다른 라이브러리를 사용하고 있었을겁니다.

nhk9680 commented 5 years ago

SSD MobileNet v2 COCO Pipeline

# SSD with Mobilenet v2 configuration for MSCOCO Dataset.
# Users should configure the fine_tune_checkpoint field in the train config as
# well as the label_map_path and input_path fields in the train_input_reader and
# eval_input_reader. Search for "PATH_TO_BE_CONFIGURED" to find the fields that
# should be configured.

model {
  ssd {
    num_classes: 3
    box_coder {
      faster_rcnn_box_coder {
        y_scale: 10.0
        x_scale: 10.0
        height_scale: 5.0
        width_scale: 5.0
      }
    }
    matcher {
      argmax_matcher {
        matched_threshold: 0.5
        unmatched_threshold: 0.5
        ignore_thresholds: false
        negatives_lower_than_unmatched: true
        force_match_for_each_row: true
      }
    }
    similarity_calculator {
      iou_similarity {
      }
    }
    anchor_generator {
      ssd_anchor_generator {
        num_layers: 6
        min_scale: 0.2
        max_scale: 0.95
        aspect_ratios: 1.0
        aspect_ratios: 2.0
        aspect_ratios: 0.5
        aspect_ratios: 3.0
        aspect_ratios: 0.3333
      }
    }
    image_resizer {
      fixed_shape_resizer {
        height: 300
        width: 300
      }
    }
    box_predictor {
      convolutional_box_predictor {
        min_depth: 0
        max_depth: 0
        num_layers_before_predictor: 0
        use_dropout: false
        dropout_keep_probability: 0.8
        kernel_size: 1
        box_code_size: 4
        apply_sigmoid_to_scores: false
        conv_hyperparams {
          activation: RELU_6,
          regularizer {
            l2_regularizer {
              weight: 0.00004
            }
          }
          initializer {
            truncated_normal_initializer {
              stddev: 0.03
              mean: 0.0
            }
          }
          batch_norm {
            train: true,
            scale: true,
            center: true,
            decay: 0.9997,
            epsilon: 0.001,
          }
        }
      }
    }
    feature_extractor {
      type: 'ssd_mobilenet_v2'
      min_depth: 16
      depth_multiplier: 1.0
      conv_hyperparams {
        activation: RELU_6,
        regularizer {
          l2_regularizer {
            weight: 0.00004
          }
        }
        initializer {
          truncated_normal_initializer {
            stddev: 0.03
            mean: 0.0
          }
        }
        batch_norm {
          train: true,
          scale: true,
          center: true,
          decay: 0.9997,
          epsilon: 0.001,
        }
      }
    }
    loss {
      classification_loss {
        weighted_sigmoid {
        }
      }
      localization_loss {
        weighted_smooth_l1 {
        }
      }
      hard_example_miner {
        num_hard_examples: 3000
        iou_threshold: 0.99
        loss_type: CLASSIFICATION
        max_negatives_per_positive: 3
        min_negatives_per_image: 3
      }
      classification_weight: 1.0
      localization_weight: 1.0
    }
    normalize_loss_by_num_matches: true
    post_processing {
      batch_non_max_suppression {
        score_threshold: 1e-8
        iou_threshold: 0.6
        max_detections_per_class: 100
        max_total_detections: 100
      }
      score_converter: SIGMOID
    }
  }
}

train_config: {
  batch_size: 12
  optimizer {
    rms_prop_optimizer: {
      learning_rate: {
        exponential_decay_learning_rate {
          initial_learning_rate: 0.004
          decay_steps: 800720
          decay_factor: 0.95
        }
      }
      momentum_optimizer_value: 0.9
      decay: 0.9
      epsilon: 1.0
    }
  }
  fine_tune_checkpoint: "/content/models/research/pretrained_model/model.ckpt"
  fine_tune_checkpoint_type:  "detection"
  # Note: The below line limits the training process to 200K steps, which we
  # empirically found to be sufficient enough to train the pets dataset. This
  # effectively bypasses the learning rate schedule (the learning rate will
  # never decay). Remove the below line to train indefinitely.
  num_steps: 1000
  data_augmentation_options {
    random_horizontal_flip {
    }
  }
  data_augmentation_options {
    ssd_random_crop {
    }
  }
}

train_input_reader: {
  tf_record_input_reader {
    input_path: "/content/object_detection_demo/data/annotations/train.record"
  }
  label_map_path: "/content/object_detection_demo/data/annotations/label_map.pbtxt"
}

eval_config: {
  num_examples: 8000
  # Note: The below line limits the evaluation process to 10 evaluations.
  # Remove the below line to evaluate indefinitely.
  max_evals: 10
}

eval_input_reader: {
  tf_record_input_reader {
    input_path: "/content/object_detection_demo/data/annotations/test.record"
  }
  label_map_path: "/content/object_detection_demo/data/annotations/label_map.pbtxt"
  shuffle: false
  num_readers: 1
}
nhk9680 commented 5 years ago

저장용

각종 파일 format


확장자명 용도
pb Binary Graph
pbtxt Text Graph
ckpt 학습 변수 체크포인트
record Binary 학습 데이터

freeze_graph : ckpt -> pb


참고 링크