반응형

github 소스 

 

GitHub - WongKinYiu/yolov7: Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors - GitHub - WongKinYiu/yolov7: Implementation of paper - YOLOv7: Trainable bag-of...

github.com

 

소스 다운하기 :

!git clone https://github.com/WongKinYiu/yolov7.git

이동하기 :

%cd yolov7

lib 설치하기

!pip install -U -r requirements.txt

1. Yolov7 coco dataset 학습하기 

 

train: WARNING: Ignoring corrupted image and/or label coco/images/train2017/000000189778.jpg: [Errno 2] No such file or directory: 'coco/images/train2017/000000189778.jpg'

./data/coco.yaml을 수정한다. 

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ./data/coco/train2017.txt  # 118287 images
val: ./data/coco/val2017.txt  # 5000 images
test: ./data/coco/test-dev2017.txt  # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794

 

github에서 수정하지 한다 .

!python train.py --workers 8 --device 0 --batch-size 32 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml

정상적으로 수행하는 것을 확인할 수 있다. 

default 값은 아래와 같다. 

YOLOR 🚀 v0.1-115-g072f76c torch 1.12.1+cu113 CUDA:0 (Tesla T4, 15109.75MB) Namespace(adam=False, artifact_alias='latest', batch_size=32, bbox_interval=-1, bucket='', cache_images=False, cfg='cfg/training/yolov7.yaml', data='data/coco.yaml', device='0', entity=None, epochs=300, evolve=False, exist_ok=False, freeze=[0], global_rank=-1, hyp='data/hyp.scratch.p5.yaml', image_weights=False, img_size=[640, 640], label_smoothing=0.0, linear_lr=False, local_rank=-1, multi_scale=False, name='yolov7', noautoanchor=False, nosave=False, notest=False, project='runs/train', quad=False, rect=False, resume=False, save_dir='runs/train/yolov7', save_period=-1, single_cls=False, sync_bn=False, total_batch_size=32, upload_dataset=False, v5_metric=False, weights='', workers=8, world_size=1) tensorboard: Start with 'tensorboard --logdir runs/train', view at http://localhost:6006/ hyperparameters: lr0=0.01, lrf=0.1, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.3, cls_pw=1.0, obj=0.7, obj_pw=1.0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.2, scale=0.9, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.15, copy_paste=0.0, paste_in=0.15, loss_ota=1 wandb: Install Weights & Biases for YOLOR logging with 'pip install wandb' (recommended)

여기에서 알 수 있다 싶이 epochs 가 300이다 . 

1-1. epochs 수정해서 수행하기 

!python train.py --workers 8 --epochs 1 --device 0 --batch-size 32 --data data/coco.yaml --img 640 640 --cfg cfg/training/yolov7.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml

 

반응형

'Deep learning > 소스' 카테고리의 다른 글

yolov5 Multi-GPU Training  (0) 2021.09.22
yolov5 coco data set training  (0) 2021.09.17
classification mnist-LeNet-5  (0) 2021.04.03
yolov5 모델pt  (0) 2021.03.26
classification model code  (0) 2021.01.14
반응형

https://github.com/ultralytics/yolov5/issues/475

 

Multi-GPU Training 🌟 · Issue #475 · ultralytics/yolov5

📚 This guide explains how to properly use multiple GPUs to train a dataset with YOLOv5 🚀 on single or multiple machine(s). UPDATED 18 August 2021. Before You Start Clone repo and install requiremen...

github.com

 

1. DataParallel

 

시간 확인 : 0.293 hours

 

2. DistributedDataParallel

시간 확인 : 0.269 hours

 

DistributedDataParallel 를 추천 한다고 되여 있는데 시간도 빠르다.

 

 

DataParallel와  DistributedDataParallel 의 비교 :

https://algopoolja.tistory.com/56

 

torch.distributed

torch로 병렬화를 하기 위해서 torch에서 제안하는 몇가지 선택할 수 있는 선택지가 있다. 1. torch.nn.DataParallel DataParallel은 하나의 본체에서 multi-GPU 병렬화를 코딩을 많이 하지 않고 할 수 있는 선택.

algopoolja.tistory.com

 

반응형

'Deep learning > 소스' 카테고리의 다른 글

Yolov7 학습하기  (0) 2022.10.21
yolov5 coco data set training  (0) 2021.09.17
classification mnist-LeNet-5  (0) 2021.04.03
yolov5 모델pt  (0) 2021.03.26
classification model code  (0) 2021.01.14
반응형

1. 먼저 yolov5를 다운한다.

yolov5관련된 lib도 설치한다.

!git clone https://github.com/ultralytics/yolov5 # clone repo
%cd yolov5
!pip install -U -r requirements.txt

2. yolov5/data/coco.yaml수정

path: ./datasets/coco

경로 수정

 

3. train 실행

!python train.py --data coco.yaml --epoch 1

 

 

 

참고 사이트 :

https://stackoverflow.com/questions/55556965/importing-coco-datasets-to-google-colaboratory

 

Importing COCO datasets to google colaboratory

The COCO dataset is very large for me to upload it to google colab. Is there any way I can directly download the dataset to google colab?

stackoverflow.com

 

반응형

'Deep learning > 소스' 카테고리의 다른 글

Yolov7 학습하기  (0) 2022.10.21
yolov5 Multi-GPU Training  (0) 2021.09.22
classification mnist-LeNet-5  (0) 2021.04.03
yolov5 모델pt  (0) 2021.03.26
classification model code  (0) 2021.01.14
반응형

tensorflow mnist LeNet-5

import tensorflow as tf
from tensorflow.keras.utils import to_categorical
import numpy as np

from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,AveragePooling2D
from tensorflow.keras import Model
# Download the mnist dataset using keras
(X_train, y_train), (X_test,y_test) = tf.keras.datasets.mnist.load_data()

X_train = X_train.astype('float32')/255.0
X_test = X_test.astype('float32')/255.0

num_classes = 10
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)

X_train = X_train[:, :, :, np.newaxis]
X_test = X_test[:, :, :, np.newaxis]

print(X_train.shape,X_test.shape, y_train.shape, y_test.shape)
image_input = Input(shape=(28, 28, 1))
x = Conv2D(6, kernel_size= (5,5), strides=1,  activation = 'relu')(image_input)
x = AveragePooling2D(2)(x)
x = Conv2D(16, kernel_size= (5,5),strides=1, activation = 'relu')(x)
x = AveragePooling2D(2)(x)

x = Flatten()(x)
x= Dense(units = 120, activation = 'relu')(x)
x= Dense(units = 84, activation = 'relu')(x)
output = Dense(10, activation='softmax')(x)
model = Model(image_input, output)
model.summary()
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(X_train,y_train, epochs=100, batch_size = 8,validation_data=(X_test, y_test), verbose=1)

pytorch mnist LeNet-5 => 향후 수정 필요 ***

import numpy as np
from datetime import datetime 

import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader

from torchvision import datasets, transforms

import matplotlib.pyplot as plt

# check device
device = 'cuda' if torch.cuda.is_available() else 'cpu'
random_seed = 42
lr = 0.001
batch_size = 32
n_epochs = 15

num_class = 10
kwargs = {'num_workers': 2, 'pin_memory': True} 
mnist_train = datasets.MNIST(root='MNIST_data/', 
                          train=True, 
                          transform=transforms.ToTensor(), 
                          download=True)

mnist_test = datasets.MNIST(root='MNIST_data/', 
                         train=False, # 
                         transform=transforms.ToTensor(), 
                         download=True)

train_loader = DataLoader(dataset=mnist_train, 
                          batch_size=batch_size, 
                          shuffle=True, **kwargs)

test_loader = DataLoader(dataset=mnist_test, 
                          batch_size=batch_size, 
                          shuffle=False, **kwargs)
class MyModel(nn.Module):
    def __init__(self):
        super(MyModel, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=5, padding=2)
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=5)

        self.fc1 = nn.Linear(16 * 6 * 6, 120)
        self.dens1 = torch.nn.Linear(in_features=120, out_features=84)
        self.dens2 = torch.nn.Linear(in_features=84, out_features=10)
    def forward(self,x):
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        x = F.max_pool2d(F.relu(self.conv2(x)), (2, 2))
        x = x.view(-1, self.num_flat_features(x))

        x = x.view(x.shape[0], -1)
        x = F.relu(self.fc1(x))
        x = F.relu(self.dens1(x))
        x = self.dens2(x)
        return x
    def num_flat_features(self, x):
        #x.size() return (256, 16, 5, 5),size의 값은 (16, 5, 5),256은 batch_size
        size = x.size()[1:]       
        num_features = 1
        for s in size:
            num_features *= s
        return num_features

net = MyModel()
print(net)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=2e-3)
for _epoch in range(epoch):
    for idx, (train_x, train_label) in enumerate(train_loader):
        label_np = np.zeros((train_label.shape[0], 10))
        optimizer.zero_grad()
        predict_y = model(train_x.float())
        _error = criterion(predict_y, train_label.long())
        if idx % 10 == 0:
            print('idx: {}, _error: {}'.format(idx, _error))
        _error.backward()
        optimizer.step()

    correct = 0
    _sum = 0

    for idx, (test_x, test_label) in enumerate(test_loader):
        predict_y = model(test_x.float()).detach()
        predict_ys = np.argmax(predict_y, axis=-1)
        label_np = test_label.numpy()
        _ = predict_ys == test_label
        correct += np.sum(_.numpy(), axis=-1)
        _sum += _.shape[0]

    print('accuracy: {:.2f}'.format(correct / _sum))

 

반응형

'Deep learning > 소스' 카테고리의 다른 글

yolov5 Multi-GPU Training  (0) 2021.09.22
yolov5 coco data set training  (0) 2021.09.17
yolov5 모델pt  (0) 2021.03.26
classification model code  (0) 2021.01.14
python 한국 시간으로 설정  (0) 2020.11.17
반응형

yolov5의 모델 그냥 저장시 무거워서 dict까지 저장하면 가벼워진다.

from models.yolo import Model
import torch
device = torch.device("cpu")
yaml_path='./dataset/test.yaml'
#yaml_path = './models/yolov5m.yaml'
new_weights='./best.pt'
model = Model(yaml_path).to(device)
trained_model = torch.load(new_weights,map_location=device)['model'].to(device)
torch.save(trained_model, 'best_1.pt')

 

오류 처리시 사용한 yaml의 내용을 참조하여 추가하면 된다.

반응형

'Deep learning > 소스' 카테고리의 다른 글

yolov5 coco data set training  (0) 2021.09.17
classification mnist-LeNet-5  (0) 2021.04.03
classification model code  (0) 2021.01.14
python 한국 시간으로 설정  (0) 2020.11.17
20201110-yolov5로 데이터 학습  (2) 2020.11.04
반응형

1. LeNet-5 코드 

1-1 LeNet-5 코드 function api 

from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,AveragePooling2D
from tensorflow.keras import Model

image_input = Input(shape=(32, 32, 1))
x = Conv2D(6, kernel_size= (5,5), strides=1,  activation = 'relu')(image_input)
x = AveragePooling2D(2)(x)
x = Conv2D(16, kernel_size= (5,5),strides=1, activation = 'relu')(x)
x = AveragePooling2D(2)(x)

x = Flatten()(x)
x= Dense(units = 120, activation = 'relu')(x)
x= Dense(units = 84, activation = 'relu')(x)
output = Dense(10, activation='softmax')(x)
model = Model(image_input, output)
model.summary()

 

1-2 LeNet-5 

import keras 
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Flatten
from keras.layers import Dense

model = Sequential()

#Layer 1
#Conv Layer 1
model.add(Conv2D(filters = 6, kernel_size = 5, strides = 1, activation = 'relu', input_shape = (32,32,1)))

#Pooling layer 1
model.add(MaxPooling2D(pool_size = 2, strides = 2))

#Layer 2
#Conv Layer 2
model.add(Conv2D(filters = 16, kernel_size = 5,strides = 1,activation = 'relu'))

#Pooling Layer 2
model.add(MaxPooling2D(pool_size = 2, strides = 2))

#Flatten
model.add(Flatten())

#Layer 3
#Fully connected layer 1
model.add(Dense(units = 120, activation = 'relu'))

#Layer 4
#Fully connected layer 2
model.add(Dense(units = 84, activation = 'relu'))

#Layer 5
#Output Layer
model.add(Dense(units = 10, activation = 'softmax'))
model.summary()

 

AlexNet keras 코드

# 5 convolutional laysers, 3 maxpooling , 3 fully connected
import keras 
from keras.models import Sequential
from keras.layers import Convolution2D
from keras.layers import MaxPooling2D,Dropout
from keras.layers import Flatten
from keras.layers import Dense

model = Sequential()
model.add(Convolution2D(96, (11, 11), strides=(4, 4), input_shape = (224,224,3),padding='SAME',  activation='relu'))
model.add(MaxPooling2D((3,3) , strides = 2))

model.add(Convolution2D(256, (5, 5), padding = 'same', activation='relu'))
model.add(MaxPooling2D((3,3) , strides = 2))

model.add(Convolution2D(384, (3, 3), padding = 'same', activation='relu'))

model.add(Convolution2D(384, (3, 3), padding = 'same', activation='relu'))

model.add(Convolution2D(256, (3, 3), padding = 'same', activation='relu'))
model.add(MaxPooling2D((3,3) , strides = 1))

model.add(Flatten())
model.add(Dense(4096, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(4096, activation = 'relu'))
model.add(Dropout(0.5))
model.add(Dense(1000, activation = 'softmax'))

model.summary()

 

VGG코드

VGG16 tensorlfow 코드

import tensorflow as tf
model = tf.keras.applications.VGG16(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000,
    classifier_activation='softmax'
)
model.summary()

block 1 2 1

block 2 2 1

block 3 2 1

block 4 2 1

block 5 3 1

flatten

dense 3

 

from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,MaxPooling2D
from tensorflow.keras import Model

image_input = Input(shape=(224, 224, 3))
x = Conv2D(64, kernel_size= (3,3), padding='same',name='block1_conv1')(image_input)
x = Conv2D(64, kernel_size= (3,3), padding='same',name='block1_conv2')(x)
x = MaxPooling2D(2 ,name ='block1_pool')(x)

x = Conv2D(128, kernel_size= (3,3), padding='same',name='block2_conv1')(x)
x = Conv2D(128, kernel_size= (3,3), padding='same',name='block2_conv2')(x)
x = MaxPooling2D(2 ,name ='block2_pool')(x)


x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv1')(x)
x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv2')(x)
x = MaxPooling2D(2 ,name ='block3_pool')(x)

x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv1')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv2')(x)
x = MaxPooling2D(2 ,name ='block4_pool')(x)

x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv1')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv2')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv3')(x)
x = MaxPooling2D(2 ,name ='block5_pool')(x)

x = Flatten()(x)
x= Dense(units = 4096, activation = 'relu',name='fc1')(x)
x= Dense(units = 4096, activation = 'relu',name='fc2')(x)
output = Dense(1000, activation='softmax' ,name='predictions')(x)
model = Model(image_input, output)
model.summary()

www.tensorflow.org/api_docs/python/tf/keras/applications/VGG16

 

tf.keras.applications.VGG16  |  TensorFlow Core v2.4.1

Instantiates the VGG16 model.

www.tensorflow.org

VGG19 tensorlfow 코드

import tensorflow as tf
model = tf.keras.applications.VGG19(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000,
    classifier_activation='softmax'
)
model.summary()
from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,MaxPooling2D
from tensorflow.keras import Model

image_input = Input(shape=(224, 224, 3))
x = Conv2D(64, kernel_size= (3,3), padding='same',name='block1_conv1')(image_input)
x = Conv2D(64, kernel_size= (3,3), padding='same',name='block1_conv2')(x)
x = MaxPooling2D(2 ,name ='block1_pool')(x)

x = Conv2D(128, kernel_size= (3,3), padding='same',name='block2_conv1')(x)
x = Conv2D(128, kernel_size= (3,3), padding='same',name='block2_conv2')(x)
x = MaxPooling2D(2 ,name ='block2_pool')(x)


x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv1')(x)
x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv2')(x)
x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv3')(x)
x = Conv2D(256, kernel_size= (3,3), padding='same',name='block3_conv4')(x)
x = MaxPooling2D(2 ,name ='block3_pool')(x)

x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv1')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv2')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv3')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block4_conv4')(x)
x = MaxPooling2D(2 ,name ='block4_pool')(x)

x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv1')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv2')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv3')(x)
x = Conv2D(512, kernel_size= (3,3), padding='same',name='block5_conv4')(x)
x = MaxPooling2D(2 ,name ='block5_pool')(x)

x = Flatten()(x)
x= Dense(units = 4096, activation = 'relu',name='fc1')(x)
x= Dense(units = 4096, activation = 'relu',name='fc2')(x)
output = Dense(1000, activation='softmax' ,name='predictions')(x)
model = Model(image_input, output)
model.summary()

ResNet코드

ResNet50 tensorflow code

model = tf.keras.applications.ResNet50(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000
)
model.summary()

 

ResNet101 tensorflow code

model = tf.keras.applications.ResNet101(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000
)
model.summary()

ResNet152 tensorflow code

model = tf.keras.applications.ResNet152(
    include_top=True, weights='imagenet', input_tensor=None,
    input_shape=None, pooling=None, classes=1000
)
model.summary()

 

keras.io/api/applications/

 

Keras documentation: Keras Applications

Keras Applications Keras Applications are deep learning models that are made available alongside pre-trained weights. These models can be used for prediction, feature extraction, and fine-tuning. Weights are downloaded automatically when instantiating a mo

keras.io

resnet 50 tensorflow 

 

github.com/keras-team/keras-applications/blob/bc89834ed36935ab4a4994446e34ff81c0d8e1b7/keras_applications/resnet50.py#L37

def conv_block(input_tensor,
               kernel_size,
               filters,
               stage,
               block,
               strides=(2, 2)):
    bn_axis = 3
    filters1, filters2, filters3 = filters
    conv_name_base = 'conv'+str(stage)+'_block' + block 

    bn_name_base = 'conv' + str(stage) +'_block'+ block
    relu_name_base = 'conv' + str(stage) +'_block'+ block

    x = layers.Conv2D(filters1, (1, 1), strides=strides,
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_1_conv')(input_tensor)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_1_bn')(x)
    x = layers.Activation('relu',name = relu_name_base+'_1_relu')(x)

    x = layers.Conv2D(filters2, kernel_size, padding='same',
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_2_conv')(x)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_2_bn')(x)
    x = layers.Activation('relu',name = relu_name_base+'_2_relu')(x)

    x = layers.Conv2D(filters3, (1, 1),
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_0_conv')(x)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_0_bn')(x)

    shortcut = layers.Conv2D(filters3, (1, 1), strides=strides,
                             kernel_initializer='he_normal',
                             name=conv_name_base + '_3_conv')(input_tensor)
    shortcut = layers.BatchNormalization(
        axis=bn_axis, name=bn_name_base + '_3_bn')(shortcut)

    x = layers.add([x, shortcut] ,name = 'conv'+str(stage)+'_block'+block+'_add')
    x = layers.Activation('relu',name = 'conv'+str(stage)+'_block'+block+'_out')(x)
    return x
 

def identity_block(input_tensor, kernel_size, filters, stage, block):
    """The identity block is the block that has no conv layer at shortcut.
    # Arguments
        input_tensor: input tensor
        kernel_size: default 3, the kernel size of
            middle conv layer at main path
        filters: list of integers, the filters of 3 conv layer at main path
        stage: integer, current stage label, used for generating layer names
        block: 'a','b'..., current block label, used for generating layer names
    # Returns
        Output tensor for the block.
    """
    filters1, filters2, filters3 = filters
    bn_axis = 3
    conv_name_base = 'conv' + str(stage) +'_block' + '_'+block
    bn_name_base = 'conv' + str(stage) +'_block' + block
    relu_name_base = 'conv' + str(stage) +'_block'+ block

    x = layers.Conv2D(filters1, (1, 1),
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_1_conv')(input_tensor)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_1_bn')(x)
    x = layers.Activation('relu',name = relu_name_base+'_1_relu')(x)

    x = layers.Conv2D(filters2, kernel_size,
                      padding='same',
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_2_conv')(x)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_2_bn')(x)
    x = layers.Activation('relu',name = relu_name_base+'_2_relu')(x)

    x = layers.Conv2D(filters3, (1, 1),
                      kernel_initializer='he_normal',
                      name=conv_name_base + '_3_conv')(x)
    x = layers.BatchNormalization(axis=bn_axis, name=bn_name_base + '_3_bn')(x)

    x = layers.add([x, input_tensor],name = 'conv'+str(stage)+'_block'+block+'_add')
    x = layers.Activation('relu',name = 'conv'+str(stage)+'_block'+block+'_out')(x)
    return x

from tensorflow.keras.layers import Conv2D,Dense,Flatten,Input,MaxPooling2D,ZeroPadding2D,BatchNormalization,Activation
from tensorflow.keras import Model
from tensorflow.keras import layers

image_input = Input(shape=(224, 224, 3))
x = ZeroPadding2D(3,name='conv1_pad')(image_input)
x = Conv2D(64, kernel_size= (7,7), strides=(2, 2),padding='valid',name='conv1_conv')(x) 
x = BatchNormalization(axis=3, name='bn_conv1')(x) 
x = Activation('relu', name='conv1_relu')(x)    
x = ZeroPadding2D(padding=(1, 1),name='pool1_pad')(x)      
x = MaxPooling2D((3, 3), strides=(2, 2),name='pool1_pool')(x) 
               
x = conv_block(x, 3, [64, 64, 256], stage=2, block='1', strides=(1, 1))
x = identity_block(x, 3, [64, 64, 256], stage=2, block='2')
x = identity_block(x, 3, [64, 64, 256], stage=2, block='3')

x = conv_block(x, 3, [128, 128, 512], stage=3, block='1')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='2')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='3')
x = identity_block(x, 3, [128, 128, 512], stage=3, block='4')

x = conv_block(x, 3, [256, 256, 1024], stage=4, block='1')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='2')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='3')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='4')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='5')
x = identity_block(x, 3, [256, 256, 1024], stage=4, block='6')

x = conv_block(x, 3, [512, 512, 2048], stage=5, block='1')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='2')
x = identity_block(x, 3, [512, 512, 2048], stage=5, block='3')

x = layers.GlobalAveragePooling2D(name='avg_pool')(x)

#https://github.com/keras-team/keras-applications/blob/master/keras_applications/resnet50.py

x = Flatten()(x)
x= Dense(units = 4096, activation = 'relu',name='fc1')(x)
x= Dense(units = 4096, activation = 'relu',name='fc2')(x)
output = Dense(1000, activation='softmax' ,name='predictions')(x)
model = Model(image_input, output)
print(model.summary())
반응형

'Deep learning > 소스' 카테고리의 다른 글

yolov5 coco data set training  (0) 2021.09.17
classification mnist-LeNet-5  (0) 2021.04.03
yolov5 모델pt  (0) 2021.03.26
python 한국 시간으로 설정  (0) 2020.11.17
20201110-yolov5로 데이터 학습  (2) 2020.11.04
반응형

from dateutil import tz

from datetime import datetime

 

utc_zone = tz.gettz('UTC')

seoul_zone = tz.gettz('Asia/Seoul')

 

utc = datetime.now()

current_time = utc.replace(tzinfo=utc_zone).astimezone(seoul_zone)

 

포맷형식 바꾸기 

nowstr = current_time.strftime('%Y-%m-%d %H:%M:%S')

 

 

taeoh.tistory.com/60

 

python utc시간 변경

python 실행 중인 컴퓨터 시간형식이 현재시간과 다른경우가 왕왕 있다. 그럴때(시스템 시간을 바꾸지 못하는 경우) 아래 코드로 강제 변환해주면 된다. from datetime import datetime from dateutil import tz..

taeoh.tistory.com

 

반응형

'Deep learning > 소스' 카테고리의 다른 글

yolov5 coco data set training  (0) 2021.09.17
classification mnist-LeNet-5  (0) 2021.04.03
yolov5 모델pt  (0) 2021.03.26
classification model code  (0) 2021.01.14
20201110-yolov5로 데이터 학습  (2) 2020.11.04
반응형

동영상 참조하여 정리 

 

데이터 다운

public.roboflow.com/ 

 

Computer Vision Datasets

Download free, open source datasets for computer vision machine learning models in a variety of formats.

public.roboflow.com

필요한 패키지 설치하기

 

 

 

runtime 유형 변경

하드웨어 가속기를 gpu로 사용한다.

label 이 x , y , width , height 로 지정되여있다.

classs ,x,y , width , height

 

yolov5 모델 불러오기

yolov4 를 github에서 받는다.

%cd /content
!git clone https://github.com/ultralytics/yolov5.git

yolov5 디렉토리로 이동한다

%cd /content/yolov5/

 

필요한 패키지 설치하기

!pip install -U -r requirements.txt

yaml 확인하기

%cat dataset/dataset40.yaml

 

포함 하고 있는것은

train 경로 

val경로

nc : class종류

mame : class 이름

 

경로가 잘못 될 경우 수정해야 하고 train val 나누기

 

 

데이터 모으기

from glob import glob

img_list = glob("/content/yolov5/dataSet/*/*.jpg")
print(len(img_list))

 

train validation set으로 나누기 

from sklearn.model_selection import train_test_split

train_img_list, val_img_list = train_test_split(img_list, test_size = 0.1, random_state=2000)
print(len(train_img_list), len(val_img_list))

train image랑 validation 이미지 경로 를  txt 파일로 저장하기 

with open("/content/yolov5/dataset/val.txt", "w") as f:
    f.write('\n'.join(train_img_list) + '\n')

with open("/content/yolov5/dataset/train.txt", "w") as f:
    f.write('\n'.join(val_img_list) + '\n')

내용을 보면

이런식으로 하위에 생긴다.

이미지 들이 한줄 씩 저장되여 있다.

 

나눈후 위에 있었든 yaml 파일을 수정

import yaml

with open('/content/yolov5/data.yaml','r') as f:
	data = yaml.load(f)
    
print(data)

json 불러오는 것과 동일하다.

 

data['train'] = '/content/yolov5/dataset/train.txt'
data['val'] = '/content/yolov5/dataset/val.txt'

with open('/content/dataset/data.yaml' ,'w') as f:
	yaml.dump(data, f)
    
print(data)

 

%cd /content/yolov5/

!python train.py --img 416 --batch 16 --epochs 50 --data /content/yolov5/data.yaml \
--cfg ./models/yolov5s.yaml --weights yolov5s.pt --name customtest

train.py : trainning 시키는 파일 실행시킨다.

github.com/ultralytics/yolov5 이 경로에서 

github.com/ultralytics/yolov5/wiki/Train-Custom-Data

cfg 는 모델의 구조를 나타난다.

yolov5/models에 정의가 되여있다.

yolov5x.yaml  가장 큰 것

yolov5l.yaml   큰 것

yolov5m.yaml 중간것

yolov5s.yaml   작은것

 

 

https://github.com/ultralytics/yolov5/issues/1289

 

Weights & Biases with YOLOv5 🌟 · Issue #1289 · ultralytics/yolov5

📚 This guide explains how to use Weights & Biases (W&B) with YOLOv5 🚀. UPDATED 25 November 2021. About Weights & Biases First-Time Setup Viewing runs Disabling wandb Advanced Usage: Dat...

github.com

weights: pretrained model 사용 한다.

name : 사용하는 폴더 이름

 

gpu 사용은 using cuda device

메모리는 15기가 정도 된다.

pretrained 모델 먼저 받은 다음에 training을 시작하게 된다.

image 개서 

targets label 개수 

P  precision

R  RECALL

mAP@.5

mAP@.5:95

 

training 되는 결과는 runs/아래에 있다.

test batch 하는 것 실시간으로 볼 수 있다.

test_batch0_pred.jpg는 예측된 결과를 보여준다.

test_batch0_gt ground truth 실제 참값이고 

train_batch 도 들어간다.

 

끝나면 precision , recall ,mAP 정보가 다나오고 

WEIGHT,가 저장 됬다는 것이 나온다.

pytorch 버전

 

 

학습 결과 result

텐스 보드 찍어본다.

%load_ext tensorboard
%tensorboad --logdir /content/yolov5/runs/

tensorboard 가 보인다.

 

 

정확도 확인

from IPython.display import Image
import os

val_img_path = val_img_list[0]

!python detect.py --weights /content/yolov5/runs/exp0_/weights/best_.pg --img 416 --conf 0.5 --sourch "{val_img_path}"

결과가 /content/yolov5/inference/output

 

보기가 힘들어서 

val_img_path = val_img_path[2]
Image(os.path.join('/content/yolov5/inference/output', os.path.basename(val_img_path)))

best weight를 다운로드 해서 한다.

 

동영상이 생성된다.

python detect.py --source ../vedio.mp4 --weights ../best_.py 

 

format에 따라 바운딩 박스는 (x, y, w, h), x, y, x+w, y+h), (x_center, y_center, w, h) 등으로 표현됩니다. YOLO v5에서는 (x_center, y_center, w, h)의 형식을 따릅니다.

 

yolov5를 학습할 경우에는 정규화를 해야 한다. 0~1 사이로 

def convert(size, box): # (XM, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin, xmin
    dw = 1./size[0]     # 1/w
    dh = 1./size[1]     # 1/h
    x = (box[0] + box[1])/2.0   # The x coordinate of the center point of the object in the graph
    y = (box[2] + box[3])/2.0   # The y coordinate of the center point of an object in a graph
    w = box[1] - box[0]         # Actual pixel width of object
    h = box[3] - box[2]         # Actual pixel height of object
    x = x*dw    # The coordinate ratio of the center point x of the object (equivalent to x / original image w)
    w = w*dw    # Width ratio of object width (equivalent to w / original image w)
    y = y*dh    # The coordinate ratio of the center point y of the object (equivalent to Y / original image h)
    h = h*dh    # Width ratio of object width (equivalent to h / original image h)
    return (x, y, w, h)    # Returns the x-coordinate ratio, y-coordinate ratio, width ratio, height ratio of the center point of the object relative to the original image, and the value range [0-1]


# Year = 2012 ', the id (file name) of the corresponding image
def convert_annotation(image_id):
    '''
    The xml File to label Documents, xml The file contains the corresponding bunding Box and picture length, size and other information,
    Through the analysis, and then normalization, finally read label In the document, that is to say
    One image file corresponds to one xml Then, through parsing and normalization, the corresponding information can be saved to a unique one label In the file
    labal Format in file: calss x y w h  At the same time, there are multiple categories corresponding to an image, so the correspondingbundingThere are also multiple messages
    '''
    # Find the corresponding folder through year and open the corresponding image_id, which corresponds to the bund file
    in_file = open('data/Annotations/%s.xml' % (image_id), encoding='utf-8')
    # Prepare the corresponding image_ Write the corresponding label s in ID, which are
    # <object-class> <x> <y> <width> <height>
    out_file = open('data/labels/%s.txt' % (image_id), 'w', encoding='utf-8')
    # Parsing xml file
    tree = ET.parse(in_file)
    # Get the corresponding key value pair
    root = tree.getroot()
    # Get the size of the picture
    size = root.find('size')
    # Get wide
    w = int(size.find('width').text)
    # Get high
    h = int(size.find('height').text)
    # Traverse obj
    for obj in root.iter('object'):
        # Get difficult??
        difficult = obj.find('difficult').text
        # Get category = string type
        cls = obj.find('name').text
        # If the category does not correspond to our scheduled class file, or difficult==1, skip
        if cls not in classes or int(difficult) == 1:
            continue
        # id found by category name
        cls_id = classes.index(cls)
        # Find the bndbox object
        xmlbox = obj.find('bndbox')
        # Get the corresponding array of bndbox = ['xmin','xmax','ymin','ymax']
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        print(image_id, cls, b)
        # Bring in for normalization operation
        # w = width, h = height, B = array of bndbox = ['xmin','xmax','ymin','ymax']
        bb = convert((w, h), b)
        # bb corresponds to normalized (x,y,w,h)
        # Generate calss x y w h in the label file
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')

- xmin: x-coordinate of the bottom left part of the image

- xmax: x-coordinate of the top right part of the image

- ymin: y-coordinate of the bottom left part of the image

- ymax: y-coordinate of the top right part of the image

 

- labels : Encoded cell-type (Yolo - label input-1)

- width : width of that bbox

- height : height of that bbox

- x_center : bbox center (x-axis)

- y_center : bbox center (y-axis)

 

- x_center_norm : x_center normalized (0-1) (Yolo - label input-2)

- y_center_norm : y_center normalized (0-1) (Yolo - label input-3)

- width_norm : width normalized (0-1) (Yolo - label input-4)

- height_norm : height normalized (0-1) (Yolo - label input-5)

(labels, x_center_norm, y_center_norm, width_norm, height_norm)

 

towardsai.net/p/computer-vision/yolo-v5-object-detection-on-a-custom-dataset

 

Yolo-v5 Object Detection on a custom dataset.

 

towardsai.net

 

출처 :

www.fatalerrors.org/a/2tp10Q.html

 

YOLOv5 realizes target detection (train one's own data set to realize cat and cat recognition)

1, Summary On June 10, 2020, Ultralytics officially released YOLOv5 on github. YOLO series can be said to be the front line of power flow in single machine target detection framework. YOLOv5 is not a single model, but a model family, including yolov5s (min

www.fatalerrors.org

 

www.youtube.com/watch?v=T0DO1C8uYP8

 

반응형

'Deep learning > 소스' 카테고리의 다른 글

yolov5 coco data set training  (0) 2021.09.17
classification mnist-LeNet-5  (0) 2021.04.03
yolov5 모델pt  (0) 2021.03.26
classification model code  (0) 2021.01.14
python 한국 시간으로 설정  (0) 2020.11.17

+ Recent posts