saicoco commented on issue #16298: [Gluon with estimator] loss is unchanged? URL: https://github.com/apache/incubator-mxnet/issues/16298#issuecomment-536157135 Here is my network: ``` import mxnet as mx from mxnet.gluon import data, HybridBlock, nn import pandas as pd import cv2 import os import numpy as np from mxnet.gluon.data.vision import transforms from mxnet.gluon.model_zoo import vision from mxnet.lr_scheduler import CosineScheduler from mxnet.gluon import loss, Trainer from mxnet import autograd import random from PIL import Image, ImageOps, ImageFilter from mxnet import nd as F, lr_scheduler as lrs from mxnet.gluon.contrib.estimator import Estimator class SteelDataset(data.Dataset): def __init__(self, df, img_dir, debug=False): self.train_df = df self.root_dir = img_dir self.transform = transforms.Compose( [ transforms.ToTensor(), transforms.Normalize( mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225) ) ] ) self.debug = debug def __getitem__(self, i): if self.debug: curr_df = self.train_df.head(20) masks = np.zeros((4, 256, 1600), np.uint8) img_names = [] item = self.train_df.iloc[i, :] img_name = item['ImageId'] for j in range(4): curr_item = item["e{}".format(j+1)] if len(curr_item) > 0: rle_pixels = curr_item label = rle_pixels.split(" ") positions = list(map(int, label[0::2])) length = list(map(int, label[1::2])) mask = np.zeros(256 * 1600, dtype=np.uint8) for pos, le in zip(positions, length): mask[pos - 1:(pos + le - 1)] = 1 masks[j, :, :] = mask.reshape(256, 1600, order='F') if self.debug: oimg = cv2.imread(os.path.join(self.root_dir, img_name))[:, :, ::-1] img = F.array(oimg) else: img = mx.image.imread(os.path.join(self.root_dir, img_name)) img = self.transform(img) if self.debug: return img, F.array(masks[:, ::4, ::4]), oimg, masks, curr_df else: return img, F.array(masks[:, ::4, ::4]) def __len__(self): return len(self.train_df) class convbn_block(HybridBlock): def __init__(self, in_channels, prefix): super().__init__(prefix=prefix) convbn = nn.HybridSequential(prefix=prefix) with convbn.name_scope(): convbn.add(nn.BatchNorm()) convbn.add(nn.Activation('relu')) convbn.add(nn.Conv2D(in_channels//4, 1)) convbn.add(nn.BatchNorm()) convbn.add(nn.Activation('relu')) convbn.add(nn.Conv2D(in_channels//4, 3, padding=(1, 1))) convbn.add(nn.BatchNorm()) convbn.add(nn.Activation('relu')) convbn.add(nn.Conv2D(in_channels, 1)) self.bk = convbn def hybrid_forward(self, F, x): return self.bk(x) class SteelUnet(HybridBlock): def __init__(self, n_classes=4, ctx=mx.cpu()): super().__init__() # mb2 = vision.mobilenet_v2_1_0(pretrained=True, ctx=ctx) # # 128x128, 64x64, 32x32, 16x16 if input_size is 512x512 # self.features = [mb2.features[:5], mb2.features[5:8], mb2.features[8:15], mb2.features[15:19]] res18 = vision.resnet18_v2(pretrained=True, ctx=ctx) features = res18.features # for param in features.collect_params().values(): # param.grad_req='null' features = [features[:5], features[5:7], features[7:8], features[8:9]] self.features = nn.HybridSequential(prefix='features') for f in features: self.features.add(f) self.convbn1 = convbn_block(512, 'convbn1') self.convbn2 = convbn_block(256, 'convbn2') self.convbn3 = convbn_block(128, 'convbn3') self.convbn4 = convbn_block(64, 'convbn4') self.head = nn.Conv2D(n_classes, 1, prefix='convbn_head') # self.convbn1.initialize(ctx=ctx) # self.convbn1.collect_params().setattr('lr_mult', 10) # self.convbn2.initialize(ctx=ctx) # self.convbn2.collect_params().setattr('lr_mult', 10) # self.convbn3.initialize(ctx=ctx) # self.convbn3.collect_params().setattr('lr_mult', 10) # self.convbn4.initialize(ctx=ctx) # self.convbn4.collect_params().setattr('lr_mult', 10) # self.head.initialize(ctx=ctx) # self.head.collect_params().setattr('lr_mult', 10) def hybrid_forward(self, F, x): c1 = self.features[0](x) c2 = self.features[1](c1) c3 = self.features[2](c2) c4 = self.features[3](c3) c4 = self.convbn1(c4) upsample_c4 = F.contrib.BilinearResize2D(c4, scale_height=2, scale_width=2) c3 = F.Activation(c3, act_type='relu') c3 = self.convbn2(F.concat(upsample_c4, c3, dim=1)) upsample_c3 = F.contrib.BilinearResize2D(c3, scale_height=2, scale_width=2) c2 = F.Activation(c2, act_type='relu') c2 = self.convbn3(F.concat(upsample_c3, c2, dim=1)) upsample_c2 = F.contrib.BilinearResize2D(c2, scale_height=2, scale_width=2) c1 = F.Activation(c1, act_type='relu') c1 = self.convbn4(F.concat(upsample_c2, c1, dim=1)) out = F.sigmoid(self.head(c1)) print(out.max(), out.min()) return out ``` I add relu for bolcks of resnet18_v2, however it doesn't work..
---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
