Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dataloader modified #305

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added __pycache__/export.cpython-38.pyc
Binary file not shown.
Binary file added __pycache__/val_dual.cpython-38.pyc
Binary file not shown.
4 changes: 2 additions & 2 deletions data/hyps/hyp.scratch-high.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,5 @@ perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
flipud: 0.0 # image flip up-down (probability)
fliplr: 0.5 # image flip left-right (probability)
mosaic: 1.0 # image mosaic (probability)
mixup: 0.15 # image mixup (probability)
copy_paste: 0.3 # segment copy-paste (probability)
mixup: 0.5 # image mixup (probability)
copy_paste: 0.0 # segment copy-paste (probability)
18 changes: 18 additions & 0 deletions data/polyp_2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# COCO 2017 dataset http://cocodataset.org

# download command/URL (optional)
# download: bash ./scripts/get_coco.sh

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

# number of classes
nc: 2

# class names
names: [ 'hyperplastic', 'adenoma']
767 changes: 767 additions & 0 deletions models/.ipynb_checkpoints/yolo-checkpoint.py

Large diffs are not rendered by default.

Binary file added models/__pycache__/__init__.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/common.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/experimental.cpython-38.pyc
Binary file not shown.
Binary file added models/__pycache__/yolo.cpython-38.pyc
Binary file not shown.
28 changes: 14 additions & 14 deletions models/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ def forward(self, x):
warnings.simplefilter('ignore') # suppress torch 1.9.0 max_pool2d() warning
return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))


class ASPP(torch.nn.Module):

def __init__(self, in_channels, out_channels):
Expand Down Expand Up @@ -493,8 +493,8 @@ def forward(self, x):

import torch.nn.functional as F
from torch.nn.modules.utils import _pair


class ReOrg(nn.Module):
# yolo
def __init__(self):
Expand Down Expand Up @@ -549,17 +549,17 @@ def __init__(self, dimension=0):

def forward(self, x):
return x[0]+x[1]


class Silence(nn.Module):
def __init__(self):
super(Silence, self).__init__()
def forward(self, x):
return x


##### GELAN #####
# #### GELAN #####

class SPPELAN(nn.Module):
# spp-elan
def __init__(self, c1, c2, c3): # ch_in, ch_out, number, shortcut, groups, expansion
Expand All @@ -575,8 +575,8 @@ def forward(self, x):
y = [self.cv1(x)]
y.extend(m(y[-1]) for m in [self.cv2, self.cv3, self.cv4])
return self.cv5(torch.cat(y, 1))


class RepNCSPELAN4(nn.Module):
# csp-elan
def __init__(self, c1, c2, c3, c4, c5=1): # ch_in, ch_out, number, shortcut, groups, expansion
Expand All @@ -597,10 +597,10 @@ def forward_split(self, x):
y.extend(m(y[-1]) for m in [self.cv2, self.cv3])
return self.cv4(torch.cat(y, 1))

#################
# ################


##### YOLOR #####
# #### YOLOR #####

class ImplicitA(nn.Module):
def __init__(self, channel):
Expand All @@ -623,10 +623,10 @@ def __init__(self, channel):
def forward(self, x):
return self.implicit * x

#################
# ################


##### CBNet #####
# #### CBNet #####

class CBLinear(nn.Module):
def __init__(self, c1, c2s, k=1, s=1, p=None, g=1): # ch_in, ch_outs, kernel, stride, padding, groups
Expand All @@ -649,7 +649,7 @@ def forward(self, xs):
out = torch.sum(torch.stack(res + xs[-1:]), dim=0)
return out

#################
# ################


class DetectMultiBackend(nn.Module):
Expand Down
2 changes: 1 addition & 1 deletion models/yolo.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ def forward(self, x):
if self.training:
return x, mc, p, s
return (torch.cat([x, mc], 1), p, s) if self.export else (torch.cat([x[0], mc], 1), (x[1], mc, p, s))


class BaseModel(nn.Module):
# YOLO base model
Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
# Usage: pip install -r requirements.txt

# Base ------------------------------------------------------------------------
gitpython
ipython
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python>=4.1.1
Expand Down
7 changes: 7 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# train yolov9 models
python train_dual.py --workers 8 --device 0 --batch 16 --data data/polyp_2.yaml --img 640 --cfg models/detect/yolov9-e.yaml --weights '' --name v9-e_1c --hyp hyp.scratch-high.yaml --min-items 0 --epochs 300 --close-mosaic 0 --exist-ok --single-cls
python val_dual.py --data data/polyp_2.yaml --img 640 --batch 32 --conf 0.25 --iou 0.65 --device 0 --weights 'runs/v9-e_1c/weights/best.pt' --name v9-c_1c --exist-ok --single-cls
python train_dual.py --workers 8 --device 0 --batch 16 --data data/polyp_2.yaml --img 640 --cfg models/detect/yolov9-e.yaml --weights '' --name v9-e_2c --hyp hyp.scratch-high.yaml --min-items 0 --epochs 300 --close-mosaic 0 --exist-ok
python val_dual.py --data data/polyp_2.yaml --img 640 --batch 32 --conf 0.25 --iou 0.65 --device 0 --weights 'runs/v9-e_2c/weights/best.pt' --name v9-e_2c --exist-ok
python train_dual.py --workers 8 --device 0 --batch 16 --data data/polyp_2.yaml --img 640 --cfg models/detect/yolov9-c.yaml --weights '' --name v9-c_1c --hyp hyp.scratch-high.yaml --min-items 0 --epochs 300 --close-mosaic 0 --exist-ok --single-cls
python val_dual.py --data data/polyp_2.yaml --img 640 --batch 32 --conf 0.25 --iou 0.65 --device 0 --weights 'runs/v9-c_1c/weights/best.pt' --name v9-c_1c --exist-ok --single-cls
9 changes: 6 additions & 3 deletions train_dual.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
Path(opt.save_dir), opt.epochs, opt.batch_size, opt.weights, opt.single_cls, opt.evolve, opt.data, opt.cfg, \
opt.resume, opt.noval, opt.nosave, opt.workers, opt.freeze
callbacks.run('on_pretrain_routine_start')

# Directories
w = save_dir / 'weights' # weights dir
(w.parent if evolve else w).mkdir(parents=True, exist_ok=True) # make dir
Expand Down Expand Up @@ -117,7 +116,6 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
else:
model = Model(cfg, ch=3, nc=nc, anchors=hyp.get('anchors')).to(device) # create
amp = check_amp(model) # check AMP

# Freeze
freeze = [f'model.{x}.' for x in (freeze if len(freeze) > 1 else range(freeze[0]))] # layers to freeze
for k, v in model.named_parameters():
Expand Down Expand Up @@ -285,7 +283,11 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
if RANK in {-1, 0}:
pbar = tqdm(pbar, total=nb, bar_format=TQDM_BAR_FORMAT) # progress bar
optimizer.zero_grad()
#tt0 = time.time()*1000
for i, (imgs, targets, paths, _) in pbar: # batch -------------------------------------------------------------
#tt1 = time.time()*1000
#print("\n loading time: ", tt1-tt0)
#tt0 = tt1
callbacks.run('on_train_batch_start')
ni = i + nb * epoch # number integrated batches (since train start)
imgs = imgs.to(device, non_blocking=True).float() / 255 # uint8 to float32, 0-255 to 0.0-1.0
Expand Down Expand Up @@ -341,6 +343,7 @@ def train(hyp, opt, device, callbacks): # hyp is path/to/hyp.yaml or hyp dictio
callbacks.run('on_train_batch_end', model, ni, imgs, targets, paths, list(mloss))
if callbacks.stop_training:
return
#print("\nend: ", time.time()*1000-t_start, "ms")
# end batch ------------------------------------------------------------------------------------------------

# Scheduler
Expand Down Expand Up @@ -462,7 +465,7 @@ def parse_opt(known=False):
parser.add_argument('--optimizer', type=str, choices=['SGD', 'Adam', 'AdamW', 'LION'], default='SGD', help='optimizer')
parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode')
parser.add_argument('--workers', type=int, default=8, help='max dataloader workers (per RANK in DDP mode)')
parser.add_argument('--project', default=ROOT / 'runs/train', help='save to project/name')
parser.add_argument('--project', default=ROOT / 'runs/', help='save to project/name')
parser.add_argument('--name', default='exp', help='save to project/name')
parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment')
parser.add_argument('--quad', action='store_true', help='quad dataloader')
Expand Down
Loading