def _reset_noise(self): # noise for mixing noise = self.gen_noise().reshape(shape= (self.noiseAll_size,) + (self.train.shape[1:])) try: sigma = self.train.max(axis = 2) / float(self.SNR) / nd.array(noise[:self.train_size].asnumpy().std(axis = 2,dtype='float64'),ctx=ctx) except: sigma = self.train.max(axis = -1) / float(self.SNR) / nd.array(noise[:self.train_size].asnumpy().std(axis = -1,dtype='float64'),ctx=ctx) self.sigma = sigma signal_train = nd.divide(self.train, sigma.reshape((self.train_size,self.num_channel,-1))) data_train = signal_train + noise[:self.train_size] try: sigma = self.test.max(axis = 2) / float(self.SNR) / nd.array(noise[-self.test_size:].asnumpy().std(axis = 2,dtype='float64'),ctx=ctx) except: sigma = self.test.max(axis = -1) / float(self.SNR) / nd.array(noise[-self.test_size:].asnumpy().std(axis = -1,dtype='float64'),ctx=ctx) signal_test = nd.divide(self.test, sigma.reshape((self.test_size,self.num_channel,-1))) data_test = signal_test + noise[-self.test_size:] # noise for pure conterpart noise = self.gen_noise().reshape(shape= (self.noiseAll_size,) + (self.train.shape[1:])) X_train = Normolise_nd(nd.concat(data_train, noise[:self.train_size], dim=0), self.num_channel) try: dataset_train = gluon.data.ArrayDataset(X_train, self.y_train) except: dataset_train = gluon.data.ArrayDataset(X_train, self.y_train) self.train_data = gluon.data.DataLoader(dataset_train, self.batch_size, shuffle=True, last_batch='keep') X_test = Normolise_nd(nd.concat(data_test, noise[-self.test_size:], dim=0), self.num_channel) try: dataset_test = gluon.data.ArrayDataset(X_test, self.y_test) except: dataset_test = gluon.data.ArrayDataset(X_test, self.y_test) self.test_data = gluon.data.DataLoader(dataset_test, self.batch_size, shuffle=True, last_batch='keep')
def backward(self, req, out_grad, in_data, out_data, in_grad, aux): pred = in_data[0] ll = in_data[1] out = nd.add(pred, ll) out = nd.divide(ll, out) # out = (ll/(pred+ll))**2 out = - nd.multiply(out, out) self.assign(in_grad[0], req[0], out)
def preprocess_lab(lab): l_chan, a_chan, b_chan = nd.split(lab, axis=2, num_outputs=3) l_chan = nd.divide(l_chan, 50) - 1 a_chan = nd.divide(a_chan, 110) b_chan = nd.divide(b_chan, 110) return [l_chan, a_chan, b_chan]
import cv2
def forward(self, x): norm = (x**2).sum(axis=1, keepdims=True).sqrt() + self.eps x = F.divide(x, norm) out = self.weight.data().expand_dims(axis=0).expand_dims( axis=2).expand_dims(axis=3) * x return out
import mxnet as mx