<jupyter_start><jupyter_text>Image visualization* generator에서 나오는 이미지는 전처리 된 것* shuffle된 상태이므로 load_image(i)의 순서와는 다르다.<jupyter_code>%matplotlib inline
#The line above is necesary to show Matplotlib's plots inside a Jupyter Notebook

import cv2
from matplotlib import pyplot as plt

for i in range(10):
    batch_inputs, batch_targets = train_generator[i]
    image1 = batch_inputs[0]
    #print(np.shape(train_generator[2][1][0][0,:,4]))
    image2 = train_generator.load_image(0)
    #image2 = cv2.cvtColor(image2, cv2.COLOR_BGR2RGB)
    #image3 =tf.image.per_image_standardization(image2)
    fig,ax = plt.subplots(3,figsize=(50,50))
    #print(np.shape(image))
    ax[0].imshow(montage_rgb(image1))

    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    image1[..., 0] *= std[0]
    image1[..., 1] *= std[1]
    image1[..., 2] *= std[2]
    image1[..., 0] += mean[0]
    image1[..., 1] += mean[1]
    image1[..., 2] += mean[2]
    image1 *= 255
    ax[1].imshow(montage_rgb(image1.astype(int)))
    #print(train_generator[2][1][0][0,:,2])
    ax[2].imshow(image2)
    #ax[2].imshow(image3)
    #plt.imshow(image2)
    plt.show()
np.any(np.array([1,2,3])==1)<jupyter_output><empty_output><jupyter_text>Augmentation<jupyter_code>misc_effect
np.shape([train_generator.load_annotations(0)])<jupyter_output><empty_output><jupyter_text>Class 분포 * 'container': 4106, * 'oil tanker': 1579, * 'aircraft carrier': 57, * 'maritime vessels': 10989이미지 patch화 되었으므로 실제보다 약간 더 많음 mAP로 평가되므로 aircraft carrier의 정확도가 중요할것 같다. 어떻게 balancing 할지 고민해야함.1. data selection2. weight loss<jupyter_code>dic = {'0':0,'1':0,'2':0,'3':0}
totsize = []
shipnum = []

for i in range(train_generator.size()):
    if i%100==0:
        print(i)
    totsize += [train_generator.load_annotations(i)['totalsize']]
    shipnum += [train_generator.load_annotations(i)['num']]
    for label in train_generator.load_annotations(i)['labels']:

        dic[str(label)]+=1
print(dic)
import matplotlib.pyplot as plt
print("배 평균갯수 : ",np.array(shipnum).mean())
print("배 총 갯수 : ",np.array(shipnum).sum())
shipnum = np.sort([shipnum])[0][::-1]
plt.hist(shipnum, bins=50)
plt.xlim([1, 10])
plt.show()
for n in range(1,10):
    print("배 {}개이상인 이미지 갯수 : ".format(n),len(np.where(np.array(shipnum)>=n)[0]))


_size=[]
for size in totsize:
    _size.extend(size)
print("배 평균 크기 : ", np.array(_size).mean())
size_mean = np.array(_size).mean()
_size = np.sort([_size])[0][::-1]
print("평균보다 큰 배의 갯수 : ", len(np.where(_size>size_mean)[0]))

count = 0
for size in totsize:
    if len(np.where(size>size_mean)[0])>0 : count +=1
print(count)
o_l = []
for i in range(train_generator.size()):
    o_l.append(train_generator.object_len(i))
len(np.array(o_l)>5)
annotations = {'labels': np.empty((0,), dtype=np.int32)}

annotations['labels'] = np.concatenate(
                [annotations['labels'],[1,2,3]])
annotations
print(train_generator[2][1][0][0,:,9])

train_generator.load_image(2)
image_input = tf.keras.layers.Input(shape=[64,64,3])
image_input.shape[-1]
mean_image_subtraction(image_input)
image1[:,:,:,0] -= 103.939
image1[:,:,:,1] -= 116.779
image1[:,:,:,2] -= 123.68

print(np.mean(image1))
fig,ax = plt.subplots(1)
ax.imshow(image1[0])

def mean_image_subtraction(images, means=[123.68, 116.78, 103.94]):
    '''
    image normalization
    :param images:
    :param means:
    :return:
    '''
    num_channels = image_input.shape[-1]
    if len(means) != num_channels:
        raise ValueError('len(means) must match the number of channels')
    channels = tf.split(axis=-1, num_or_size_splits=num_channels, value=images)
    print(channels)
    for i in range(num_channels):
        channels[i] -= means[i]
    return tf.concat(axis=3, values=channels)
print(np.mean(image1))
print(np.mean(image2))
print(np.mean(image3))
print(train_generator.classes.keys())
my_dic = dict({"0":0,"1":0,"2":0,"3":0})

print(train_generator.size())
for i in range(train_generator.size()):
    if i%100==0:
        print(i)
    for label in train_generator.load_annotations(i)['labels']:
        my_dic[str(label)]+=1


my_dic
print(train_generator.load_annotations(0))
print(train_generator.load_annotations(0)['quadrangles'].astype(np.double).dtype)
from utils.compute_overlap import compute_overlap
compute_overlap(train_generator.load_annotations(0)['bboxes'].astype(np.double),train_generator.load_annotations(0)['bboxes'].astype(np.double))
validation_generator.size()
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())

tf.test.is_gpu_available(train_generator.load_annotations(0)['bboxes'],train_generator.load_annotations(0)['bboxes'])
from matplotlib.patches import Polygon,Rectangle
import colorsys
import random
import matplotlib.pyplot as plt
def random_colors(N, bright=True):
    """
    Generate random colors.
    To get visually distinct colors, generate them in HSV space then
    convert to RGB.
    """
    brightness = 1.0 if bright else 0.7
    hsv = [(i / N, 1, brightness) for i in range(N)]
    colors = list(map(lambda c: colorsys.hsv_to_rgb(*c), hsv))
    random.shuffle(colors)
    return colors
    
def visualize(image, boxes,figsize=(16, 16),box_type='rect',name=None):
        """
        desc : bbox와 함께 이미지를 그린다.
        
        -input-
        image_id : 시각화 하기를 원하는 이미지 인덱스 번호
        figsize : 이미지 크기
        """   
        fig, ax = plt.subplots(1, figsize=figsize)
        char_boxes=boxes
        char_len=len(char_boxes)
        colors = random_colors(char_len)
        print ("box channel : ", np.shape(boxes)[1])
        for i in range(char_len):
            color = colors[i]
        # Bounding box
            if not np.any(char_boxes[i]):
                # Skip this instance. Has no bbox. Likely lost in image cropping.
                continue
            
            if box_type == 'rect':
                if np.shape(boxes)[1] == 4 and len(np.shape(boxes))==3 : # 4 - vertex 
                    box = char_boxes[i]
                    y_max = max(box[0, 1], box[1, 1], box[2, 1], box[3, 1])
                    y_min = min(box[0, 1], box[1, 1], box[2, 1], box[3, 1])
                    x_max = max(box[0, 0], box[1, 0], box[2, 0], box[3, 0])
                    x_min = min(box[0, 0], box[1, 0], box[2, 0], box[3, 0])

                else : # 2 - vertex 
                    box = char_boxes[i]
                    y_max = max(box[1], box[3])
                    y_min = min(box[1], box[3])
                    x_max = max(box[0], box[2])
                    x_min = min(box[0], box[2])            
                width = (x_max-x_min)
                height = (y_max-y_min)
                print(width,height,x_min,y_min)
                p = Rectangle((x_min,y_min), width, height, linewidth=2,
                                      edgecolor=color, facecolor='none')
            elif box_type == 'quad': 
                p = Polygon(char_boxes[i], facecolor="none", edgecolor=color)
            else : 
                raise ("check the box_type")
                
            ax.add_patch(p)
        if name is not None : 
            ax.set_title(name)
        ax.imshow(image)<jupyter_output><empty_output><jupyter_text>load_image, load_annotations 확인<jupyter_code>image = train_generator.load_image(0)
bboxes = train_generator.load_annotations(0)['bboxes']
print(bboxes[0,1])
visualize(image,bboxes)<jupyter_output><empty_output><jupyter_text>annotation 확인 및 시각화<jupyter_code>for i in range(900,910):
    image = train_generator.load_image(i)
    quadboxes = train_generator.load_annotations(i)['quadrangles']
    visualize(image,quadboxes,box_type='quad')
if __name__ == '__main__':
    train_generator = ShipGenerator(
        'datasets/ship_detection',
        'train',
        phi=1,
        batch_size=1,
        detect_ship =True
    )
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]
    anchors = train_generator.anchors
    batch_inputs, batch_targets = train_generator[0]
    image = batch_inputs[0][0]
    image[..., 0] *= std[0]
    image[..., 1] *= std[1]
    image[..., 2] *= std[2]
    image[..., 0] += mean[0]
    image[..., 1] += mean[1]
    image[..., 2] += mean[2]
    image *= 255.

    regression = batch_targets[0][0]
    valid_ids = np.where(regression[:, -1] == 1)[0]
    boxes = anchors[valid_ids]
    deltas = regression[valid_ids]
    class_ids = np.argmax(batch_targets[1][0][valid_ids], axis=-1)
    mean_ = [0, 0, 0, 0]
    std_ = [0.2, 0.2, 0.2, 0.2]

    width = boxes[:, 2] - boxes[:, 0]
    height = boxes[:, 3] - boxes[:, 1]

    x1 = boxes[:, 0] + (deltas[:, 0] * std_[0] + mean_[0]) * width
    y1 = boxes[:, 1] + (deltas[:, 1] * std_[1] + mean_[1]) * height
    x2 = boxes[:, 2] + (deltas[:, 2] * std_[2] + mean_[2]) * width
    y2 = boxes[:, 3] + (deltas[:, 3] * std_[3] + mean_[3]) * height
    for x1_, y1_, x2_, y2_, class_id in zip(x1, y1, x2, y2, class_ids):
        x1_, y1_, x2_, y2_ = int(x1_), int(y1_), int(x2_), int(y2_)
        cv2.rectangle(image, (x1_, y1_), (x2_, y2_), (0, 255, 0), 2)
        class_name = train_generator.labels[class_id]
        label = class_name
        ret, baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.3, 1)
        cv2.rectangle(image, (x1_, y2_ - ret[1] - baseline), (x1_ + ret[0], y2_), (255, 255, 255), -1)
        cv2.putText(image, label, (x1_, y2_ - baseline), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
    cv2.imshow('image', image.astype(np.uint8)[..., ::-1])
    cv2.waitKey(0)
    # 36864, 46080, 48384, 48960, 49104
    # if first_valid_id < 36864:
    #     stride = 8
    # elif 36864 <= first_valid_id < 46080:
    #     stride = 16
    # elif 46080 <= first_valid_id < 48384:
    #     stride = 32
    # elif 48384 <= first_valid_id < 48960:
    #     stride = 64
    # else:
    #     stride = 128
    pass<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16507128309572303, 'fraction_numerical': 0.05213849287169043, 'mean_word_length': 3.075103734439834, 'pattern_counts': {'":': 4, '<': 28, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '361605', 'license': 'Apache-2.0', 'n_tokens_mistral': 4078, 'n_tokens_neox': 3887, 'n_words': 740, 'path': 'generators/ship_gen.ipynb', 'repo_name': 'spydr1/EfficientDet', 'size': '9820'}