Python mxnet.ndarray.transpose() Examples

The following are 30 code examples of mxnet.ndarray.transpose(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mxnet.ndarray , or try the search function .
Example #1
Source File: verification.py    From insightface with MIT License 6 votes vote down vote up
def load_bin(path, image_size):
  bins, issame_list = pickle.load(open(path, 'rb'))
  data_list = []
  for flip in [0,1]:
    data = nd.empty((len(issame_list)*2, 3, image_size[0], image_size[1]))
    data_list.append(data)
  for i in xrange(len(issame_list)*2):
    _bin = bins[i]
    img = mx.image.imdecode(_bin)
    if img.shape[1]!=image_size[0]:
      img = mx.image.resize_short(img, image_size[0])
    img = nd.transpose(img, axes=(2, 0, 1))
    for flip in [0,1]:
      if flip==1:
        img = mx.ndarray.flip(data=img, axis=2)
      data_list[flip][i][:] = img
    if i%1000==0:
      print('loading bin', i)
  print(data_list[0].shape)
  return (data_list, issame_list) 
Example #2
Source File: lfw_comparison_and_plot_roc.py    From MobileFace with MIT License 6 votes vote down vote up
def load_dataset_bin(self):
        name = 'lfw'
        path = os.path.join(self.lfw_dir, name+".bin")
        bins, issame_list = pickle.load(open(path, 'rb'))
        data_list = []
        for flip in [0,1]:
          data = nd.empty((len(issame_list)*2, 3, self.image_size[0], self.image_size[1]))
          data_list.append(data)
        for i in xrange(len(issame_list)*2):
          _bin = bins[i]
          img = mx.image.imdecode(_bin)
          img = nd.transpose(img, axes=(2, 0, 1))
          for flip in [0,1]:
            if flip==1:
              img = mx.ndarray.flip(data=img, axis=2)
            data_list[flip][i][:] = img
          if i%1000==0:
            print('loading bin', i)
        print(data_list[0].shape)
        return (data_list, issame_list) 
Example #3
Source File: score_fun.py    From dgl with Apache License 2.0 6 votes vote down vote up
def create_neg(self, neg_head):
        if neg_head:
            def fn(heads, relations, tails, num_chunks, chunk_size, neg_sample_size):
                hidden_dim = heads.shape[1]
                heads = heads.reshape(num_chunks, neg_sample_size, hidden_dim)
                heads = mx.nd.transpose(heads, axes=(0,2,1))
                tails = tails.expand_dims(2)
                relations = relations.reshape(-1, self.relation_dim, self.entity_dim)
                tmp = mx.nd.batch_dot(relations, tails).squeeze()
                tmp = tmp.reshape(num_chunks, chunk_size, hidden_dim)
                return nd.linalg_gemm2(tmp, heads)
            return fn
        else:
            def fn(heads, relations, tails, num_chunks, chunk_size, neg_sample_size):
                hidden_dim = heads.shape[1]
                tails = tails.reshape(num_chunks, neg_sample_size, hidden_dim)
                tails = mx.nd.transpose(tails, axes=(0,2,1))
                heads = heads.expand_dims(2)
                relations = relations.reshape(-1, self.relation_dim, self.entity_dim)
                tmp = mx.nd.batch_dot(relations, heads).squeeze()
                tmp = tmp.reshape(num_chunks, chunk_size, hidden_dim)
                return nd.linalg_gemm2(tmp, tails)
            return fn 
Example #4
Source File: score_fun.py    From dgl with Apache License 2.0 6 votes vote down vote up
def create_neg(self, neg_head):
        if neg_head:
            def fn(heads, relations, tails, num_chunks, chunk_size, neg_sample_size):
                hidden_dim = heads.shape[1]
                heads = heads.reshape(num_chunks, neg_sample_size, hidden_dim)
                heads = nd.transpose(heads, axes=(0, 2, 1))
                tmp = (tails * relations).reshape(num_chunks, chunk_size, hidden_dim)
                return nd.linalg_gemm2(tmp, heads)
            return fn
        else:
            def fn(heads, relations, tails, num_chunks, chunk_size, neg_sample_size):
                hidden_dim = heads.shape[1]
                tails = tails.reshape(num_chunks, neg_sample_size, hidden_dim)
                tails = nd.transpose(tails, axes=(0, 2, 1))
                tmp = (heads * relations).reshape(num_chunks, chunk_size, hidden_dim)
                return nd.linalg_gemm2(tmp, tails)
            return fn 
Example #5
Source File: verification.py    From 1.FaceRecognition with MIT License 6 votes vote down vote up
def load_bin(path, image_size):
    try:
        with open(path, 'rb') as f:
            bins, issame_list = pickle.load(f)  # py2
    except UnicodeDecodeError as e:
        with open(path, 'rb') as f:
            bins, issame_list = pickle.load(f, encoding='bytes')  # py3
    data_list = []
    for flip in [0, 1]:
        data = nd.empty((len(issame_list) * 2, 3, image_size[0], image_size[1]))
        data_list.append(data)
    for i in range(len(issame_list) * 2):
        _bin = bins[i]
        img = mx.image.imdecode(_bin)
        if img.shape[1] != image_size[0]:
            img = mx.image.resize_short(img, image_size[0])
        img = nd.transpose(img, axes=(2, 0, 1))
        for flip in [0, 1]:
            if flip == 1:
                img = mx.ndarray.flip(data=img, axis=2)
            data_list[flip][i][:] = img
        if i % 1000 == 0:
            print('loading bin', i)
    print(data_list[0].shape)
    return (data_list, issame_list) 
Example #6
Source File: verification.py    From 1.FaceRecognition with MIT License 6 votes vote down vote up
def load_bin(path, image_size):
  bins, issame_list = pickle.load(open(path, 'rb'))
  data_list = []
  for flip in [0,1]:
    data = nd.empty((len(issame_list)*2, 3, image_size[0], image_size[1]))
    data_list.append(data)
  for i in xrange(len(issame_list)*2):
    _bin = bins[i]
    img = mx.image.imdecode(_bin)
    if img.shape[1]!=image_size[0]:
      img = mx.image.resize_short(img, image_size[0])
    img = nd.transpose(img, axes=(2, 0, 1))
    for flip in [0,1]:
      if flip==1:
        img = mx.ndarray.flip(data=img, axis=2)
      data_list[flip][i][:] = img
    if i%1000==0:
      print('loading bin', i)
  print(data_list[0].shape)
  return (data_list, issame_list) 
Example #7
Source File: lfw.py    From 1.FaceRecognition with MIT License 6 votes vote down vote up
def load_dataset(lfw_dir, image_size):
  lfw_pairs = read_pairs(os.path.join(lfw_dir, 'pairs.txt'))
  lfw_paths, issame_list = get_paths(lfw_dir, lfw_pairs, 'jpg')
  lfw_data_list = []
  for flip in [0,1]:
    lfw_data = nd.empty((len(lfw_paths), 3, image_size[0], image_size[1]))
    lfw_data_list.append(lfw_data)
  i = 0
  for path in lfw_paths:
    with open(path, 'rb') as fin:
      _bin = fin.read()
      img = mx.image.imdecode(_bin)
      img = nd.transpose(img, axes=(2, 0, 1))
      for flip in [0,1]:
        if flip==1:
          img = mx.ndarray.flip(data=img, axis=2)
        lfw_data_list[flip][i][:] = img
      i+=1
      if i%1000==0:
        print('loading lfw', i)
  print(lfw_data_list[0].shape)
  print(lfw_data_list[1].shape)
  return (lfw_data_list, issame_list) 
Example #8
Source File: verification.py    From MaskInsightface with Apache License 2.0 6 votes vote down vote up
def load_bin(path, image_size):
  bins, issame_list = pickle.load(open(path, 'rb'), encoding='bytes')
  data_list = []
  for flip in [0,1]:
    data = nd.empty((len(issame_list)*2, 3, image_size[0], image_size[1]))
    data_list.append(data)
  for i in range(len(issame_list)*2):
    _bin = bins[i]
    img = mx.image.imdecode(_bin)
    img = nd.transpose(img, axes=(2, 0, 1))
    for flip in [0,1]:
      if flip==1:
        img = mx.ndarray.flip(data=img, axis=2)
      data_list[flip][i][:] = img
    if i%1000==0:
      print('loading bin', i)
  print(data_list[0].shape)
  return (data_list, issame_list) 
Example #9
Source File: lfw.py    From MaskInsightface with Apache License 2.0 6 votes vote down vote up
def load_dataset(lfw_dir, image_size):
  lfw_pairs = read_pairs(os.path.join(lfw_dir, 'pairs.txt'))
  lfw_paths, issame_list = get_paths(lfw_dir, lfw_pairs, 'jpg')
  lfw_data_list = []
  for flip in [0,1]:
    lfw_data = nd.empty((len(lfw_paths), 3, image_size[0], image_size[1]))
    lfw_data_list.append(lfw_data)
  i = 0
  for path in lfw_paths:
    with open(path, 'rb') as fin:
      _bin = fin.read()
      img = mx.image.imdecode(_bin)
      img = nd.transpose(img, axes=(2, 0, 1))
      for flip in [0,1]:
        if flip==1:
          img = mx.ndarray.flip(data=img, axis=2)
        lfw_data_list[flip][i][:] = img
      i+=1
      if i%1000==0:
        print('loading lfw', i)
  print(lfw_data_list[0].shape)
  print(lfw_data_list[1].shape)
  return (lfw_data_list, issame_list) 
Example #10
Source File: lfw.py    From insightface with MIT License 6 votes vote down vote up
def load_dataset(lfw_dir, image_size):
  lfw_pairs = read_pairs(os.path.join(lfw_dir, 'pairs.txt'))
  lfw_paths, issame_list = get_paths(lfw_dir, lfw_pairs, 'jpg')
  lfw_data_list = []
  for flip in [0,1]:
    lfw_data = nd.empty((len(lfw_paths), 3, image_size[0], image_size[1]))
    lfw_data_list.append(lfw_data)
  i = 0
  for path in lfw_paths:
    with open(path, 'rb') as fin:
      _bin = fin.read()
      img = mx.image.imdecode(_bin)
      img = nd.transpose(img, axes=(2, 0, 1))
      for flip in [0,1]:
        if flip==1:
          img = mx.ndarray.flip(data=img, axis=2)
        lfw_data_list[flip][i][:] = img
      i+=1
      if i%1000==0:
        print('loading lfw', i)
  print(lfw_data_list[0].shape)
  print(lfw_data_list[1].shape)
  return (lfw_data_list, issame_list) 
Example #11
Source File: verification.py    From insightface with MIT License 6 votes vote down vote up
def load_bin(path, image_size):
  try:
    with open(path, 'rb') as f:
      bins, issame_list = pickle.load(f) #py2
  except UnicodeDecodeError as e:
    with open(path, 'rb') as f:
      bins, issame_list = pickle.load(f, encoding='bytes') #py3
  data_list = []
  for flip in [0,1]:
    data = nd.empty((len(issame_list)*2, 3, image_size[0], image_size[1]))
    data_list.append(data)
  for i in range(len(issame_list)*2):
    _bin = bins[i]
    img = mx.image.imdecode(_bin)
    if img.shape[1]!=image_size[0]:
      img = mx.image.resize_short(img, image_size[0])
    img = nd.transpose(img, axes=(2, 0, 1))
    for flip in [0,1]:
      if flip==1:
        img = mx.ndarray.flip(data=img, axis=2)
      data_list[flip][i][:] = img
    if i%1000==0:
      print('loading bin', i)
  print(data_list[0].shape)
  return (data_list, issame_list) 
Example #12
Source File: data.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #13
Source File: age_iter.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #14
Source File: faces_classer.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def predict(self,img):
        img = nd.array(img)
        #print(img.shape)
        img = nd.transpose(img, axes=(2, 0, 1)).astype('float32')
        img = nd.expand_dims(img, axis=0)
        #print(img.shape)
        db = mx.io.DataBatch(data=(img,))

        self.model.forward(db, is_train=False)
        net_out = self.model.get_outputs()
        embedding = net_out[0].asnumpy()
        embedding = sklearn.preprocessing.normalize(embedding,axis=1)
        return embedding 
Example #15
Source File: data.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #16
Source File: data.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def next(self):
        """Returns the next batch of data."""
        #print('next')
        batch_size = self.batch_size
        batch_data = nd.empty((batch_size,)+self.data_shape)
        batch_label = nd.empty((batch_size,)+self.label_shape)
        i = 0
        #self.cutoff = random.randint(800,1280)
        try:
            while i < batch_size:
                #print('N', i)
                data, label = self.next_sample()
                data = nd.array(data)
                data = nd.transpose(data, axes=(2, 0, 1))
                label = nd.array(label)
                label = nd.transpose(label, axes=(2, 0, 1))
                batch_data[i][:] = data
                batch_label[i][:] = label
                i += 1
        except StopIteration:
            if i<batch_size:
                raise StopIteration

        #return {self.data_name  :  batch_data,
        #        self.label_name :  batch_label}
        #print(batch_data.shape, batch_label.shape)
        return mx.io.DataBatch([batch_data], [batch_label, self.weight_mask], batch_size - i) 
Example #17
Source File: image_iter.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #18
Source File: triplet_image_iter.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #19
Source File: dataset_clean.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def predict(self,img):
        img = nd.array(img)
        img = nd.transpose(img, axes=(2, 0, 1)).astype('float32')
        img = nd.expand_dims(img, axis=0)
        #print(img.shape)
        db = mx.io.DataBatch(data=(img,))

        self.model.forward(db, is_train=False)
        net_out = self.model.get_outputs()
        embedding = net_out[0].asnumpy()
        embedding = sklearn.preprocessing.normalize(embedding)
        return embedding 
Example #20
Source File: image_iter.py    From MaskInsightface with Apache License 2.0 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #21
Source File: delete_same_face.py    From 1.FaceRecognition with MIT License 5 votes vote down vote up
def predict(self, img):
        img = nd.array(img)
        img = nd.transpose(img, axes=(2, 0, 1)).astype('float32')
        img = nd.expand_dims(img, axis=0)
        # print(img.shape)
        db = mx.io.DataBatch(data=(img,))

        self.model.forward(db, is_train=False)
        net_out = self.model.get_outputs()
        embedding = net_out[0].asnumpy()
        embedding = sklearn.preprocessing.normalize(embedding)
        return embedding 
Example #22
Source File: super_resolution.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def _rearrange(raw, F, upscale_factor):
    # (N, C * r^2, H, W) -> (N, C, r^2, H, W)
    splitted = F.reshape(raw, shape=(0, -4, -1, upscale_factor**2, 0, 0))
    # (N, C, r^2, H, W) -> (N, C, r, r, H, W)
    unflatten = F.reshape(splitted, shape=(0, 0, -4, upscale_factor, upscale_factor, 0, 0))
    # (N, C, r, r, H, W) -> (N, C, H, r, W, r)
    swapped = F.transpose(unflatten, axes=(0, 1, 4, 2, 5, 3))
    # (N, C, H, r, W, r) -> (N, C, H*r, W*r)
    return F.reshape(swapped, shape=(0, 0, -3, -3)) 
Example #23
Source File: super_resolution.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def _rearrange(raw, F, upscale_factor):
    # (N, C * r^2, H, W) -> (N, C, r^2, H, W)
    splitted = F.reshape(raw, shape=(0, -4, -1, upscale_factor**2, 0, 0))
    # (N, C, r^2, H, W) -> (N, C, r, r, H, W)
    unflatten = F.reshape(splitted, shape=(0, 0, -4, upscale_factor, upscale_factor, 0, 0))
    # (N, C, r, r, H, W) -> (N, C, H, r, W, r)
    swapped = F.transpose(unflatten, axes=(0, 1, 4, 2, 5, 3))
    # (N, C, H, r, W, r) -> (N, C, H*r, W*r)
    return F.reshape(swapped, shape=(0, 0, -3, -3)) 
Example #24
Source File: triplet_image_iter.py    From insightface with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #25
Source File: data.py    From insightocr with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #26
Source File: triplet_image_iter.py    From insightface with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #27
Source File: image_iter.py    From insightface with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #28
Source File: data.py    From insightface with MIT License 5 votes vote down vote up
def next(self):
        """Returns the next batch of data."""
        #print('next')
        batch_size = self.batch_size
        batch_data = nd.empty((batch_size,)+self.data_shape)
        batch_label = nd.empty((batch_size,)+self.label_shape)
        i = 0
        #self.cutoff = random.randint(800,1280)
        try:
            while i < batch_size:
                #print('N', i)
                data, label = self.next_sample()
                data = nd.array(data)
                data = nd.transpose(data, axes=(2, 0, 1))
                label = nd.array(label)
                label = nd.transpose(label, axes=(2, 0, 1))
                batch_data[i][:] = data
                batch_label[i][:] = label
                i += 1
        except StopIteration:
            if i<batch_size:
                raise StopIteration

        #return {self.data_name  :  batch_data,
        #        self.label_name :  batch_label}
        #print(batch_data.shape, batch_label.shape)
        return mx.io.DataBatch([batch_data], [batch_label, self.weight_mask], batch_size - i) 
Example #29
Source File: age_iter.py    From insightface with MIT License 5 votes vote down vote up
def postprocess_data(self, datum):
        """Final postprocessing step before image is loaded into the batch."""
        return nd.transpose(datum, axes=(2, 0, 1)) 
Example #30
Source File: lfw_comparison_and_plot_roc.py    From MobileFace with MIT License 5 votes vote down vote up
def load_dataset(self):
        lfw_pairs = self.read_pairs(os.path.join(self.lfw_dir, 'pairs.txt'))
        lfw_paths, issame_list = self.get_paths(self.lfw_dir, lfw_pairs, 'jpg')
        lfw_data_list = []
        for flip in [0,1]:
          # lfw_data = nd.empty((len(lfw_paths), 3, image_size[0], image_size[1]))
          lfw_data = nd.empty((len(lfw_paths), 1, 100, 100))
          lfw_data_list.append(lfw_data)
        i = 0
        for path in lfw_paths:
          with open(path, 'rb') as fin:           
            _bin = fin.read()
            img = np.asarray(bytearray(_bin), dtype="uint8")
            img = cv2.imdecode(img, 0) # (100, 100)
            img = img.reshape((1, img.shape[0], img.shape[1])) # (1, 100, 100)
            #img = nd.transpose(img, axes=(2, 0, 1)) # (1L, 100L, 100L)
            img = mx.nd.array(img) # (1L, 100L, 100L)
            for flip in [0,1]:
              if flip==1:
                img = mx.ndarray.flip(data=img, axis=2)
              lfw_data_list[flip][i][:] = img
            i+=1
            if i%1000==0:
              print('loading lfw', i)
        print(lfw_data_list[0].shape)
        print(lfw_data_list[1].shape)
        return (lfw_data_list, issame_list)