Is there a problem on my CNN model between my nn.AdaptiveAvgPool2d and nn.Dropout layers? - deep-learning

I'm writting a Model to perform a classification on images for a school project.
I've 10 classes and I load images in batch on my model :
import torch
import torch.nn as nn
import torch.nn.functional as F
# *****START CODE
class ConvNet(nn.Module):
def __init__(self, in_ch, out_ch):
super(ConvNet, self).__init__()
"""
Number of layers should be exactly same as in the provided JSON.
Do not use any grouping function like Sequential
"""
self.Layer_001 = nn.Conv2d(in_channels=in_ch, out_channels=64, kernel_size=3, padding=1)
self.Layer_002 = nn.ReLU()
self.Layer_003 = nn.MaxPool2d(kernel_size=2,stride=2)
self.Layer_004 = nn.Conv2d(in_channels=64, out_channels=113, kernel_size=3, padding=1)
self.Layer_005 = nn.ReLU()
self.Layer_006 = nn.MaxPool2d(kernel_size=2,stride=2)
self.Layer_007 = nn.Conv2d(in_channels=113, out_channels=248, kernel_size=3, padding=1)
self.Layer_008 = nn.ReLU()
self.Layer_009 = nn.Conv2d(in_channels=248, out_channels=248, kernel_size=3, padding=1)
self.Layer_010 = nn.ReLU()
self.Layer_011 = nn.MaxPool2d(kernel_size=2,stride=2)
self.Layer_012 = nn.Conv2d(in_channels=248, out_channels=519, kernel_size=3, padding=1)
self.Layer_013 = nn.ReLU()
self.Layer_014 = nn.Conv2d(in_channels=519, out_channels=519, kernel_size=3, padding=1)
self.Layer_015 = nn.ReLU()
self.Layer_016 = nn.MaxPool2d(kernel_size=2,stride=2)
self.Layer_017 = nn.Conv2d(in_channels=519, out_channels=519, kernel_size=3, padding=1)
self.Layer_018 = nn.ReLU()
self.Layer_019 = nn.Conv2d(in_channels=519, out_channels=519, kernel_size=3, padding=1)
self.Layer_020 = nn.ReLU()
self.Layer_021 = nn.MaxPool2d(kernel_size=2,stride=2)
self.Layer_022 = nn.AdaptiveAvgPool2d((1,1))
self.Layer_023 = nn.Dropout(p=0.501816987002085)
self.Layer_024 = nn.Linear(in_features=519,out_features=2317)
self.Layer_025 = nn.ReLU()
self.Layer_026 = nn.Linear(in_features=2317, out_features=3018)
self.Layer_027 = nn.Linear(in_features=3018, out_features=3888)
self.Layer_028 = nn.ReLU()
self.Layer_029 = nn.Linear(in_features=3888, out_features=out_ch)
def forward(self, x):
x = self.Layer_001(x)
#print(x.shape)
x = self.Layer_002(x)
#print(x.shape)
x = self.Layer_003(x)
#print(x.shape)
x = self.Layer_004(x)
#print(x.shape)
x = self.Layer_005(x)
#print(x.shape)
x = self.Layer_006(x)
#print(x.shape)
x = self.Layer_007(x)
#print(x.shape)
x = self.Layer_008(x)
#print(x.shape)
x = self.Layer_009(x)
#print(x.shape)
x = self.Layer_009(x)
#print(x.shape)
x = self.Layer_010(x)
#print(x.shape)
x = self.Layer_011(x)
#print(x.shape)
x = self.Layer_012(x)
#print(x.shape)
x = self.Layer_013(x)
#print(x.shape)
x = self.Layer_014(x)
#print(x.shape)
x = self.Layer_015(x)
#print(x.shape)
x = self.Layer_016(x)
#print(x.shape)
x = self.Layer_017(x)
#print(x.shape)
x = self.Layer_018(x)
#print(x.shape)
x = self.Layer_019(x)
#print(x.shape)
x = self.Layer_020(x)
#print(x.shape)
x = self.Layer_021(x)
#print(x.shape)
x = self.Layer_022(x)
#print(x.shape)
x = self.Layer_023(x)
#print(x.shape)
#x = nn.Flatten(x)
##print(x.shape)
x = self.Layer_024(x)
#print(x.shape)
x = self.Layer_025(x)
#print(x.shape)
x = self.Layer_026(x)
#print(x.shape)
x = self.Layer_027(x)
#print(x.shape)
x = self.Layer_028(x)
#print(x.shape)
output = self.Layer_029(x)
print(output.shape)
return output
# *****END CODE
And when I run it I have an error with between layers
it return this error :
RuntimeError: mat1 and mat2 shapes cannot be multiplied (8304x1 and 519x2317)
I understand it a shape problem but I'm learning and don't understand where it's happening...
I'm trying to rebuild this architecture:
'Layer_001': {'input': 3,
'kernel_size': 3,
'output': 64,
'padding': 1,
'type': 'Conv2d'},
'Layer_002': {'type': 'ReLU'},
'Layer_003': {'kernel_size': 2, 'stride': 2, 'type': 'MaxPool2d'},
'Layer_004': {'input': 64,
'kernel_size': 3,
'output': 113,
'padding': 1,
'type': 'Conv2d'},
'Layer_005': {'type': 'ReLU'},
'Layer_006': {'kernel_size': 2, 'stride': 2, 'type': 'MaxPool2d'},
'Layer_007': {'input': 113,
'kernel_size': 3,
'output': 248,
'padding': 1,
'type': 'Conv2d'},
'Layer_008': {'type': 'ReLU'},
'Layer_009': {'input': 248,
'kernel_size': 3,
'output': 248,
'padding': 1,
'type': 'Conv2d'},
'Layer_010': {'type': 'ReLU'},
'Layer_011': {'kernel_size': 2, 'stride': 2, 'type': 'MaxPool2d'},
'Layer_012': {'input': 248,
'kernel_size': 3,
'output': 519,
'padding': 1,
'type': 'Conv2d'},
'Layer_013': {'type': 'ReLU'},
'Layer_014': {'input': 519,
'kernel_size': 3,
'output': 519,
'padding': 1,
'type': 'Conv2d'},
'Layer_015': {'type': 'ReLU'},
'Layer_016': {'kernel_size': 2, 'stride': 2, 'type': 'MaxPool2d'},
'Layer_017': {'input': 519,
'kernel_size': 3,
'output': 519,
'padding': 1,
'type': 'Conv2d'},
'Layer_018': {'type': 'ReLU'},
'Layer_019': {'input': 519,
'kernel_size': 3,
'output': 519,
'padding': 1,
'type': 'Conv2d'},
'Layer_020': {'type': 'ReLU'},
'Layer_021': {'kernel_size': 2, 'stride': 2, 'type': 'MaxPool2d'},
'Layer_022': {'output': 'COMPUTE', 'type': 'AdaptiveAvgPool2d'},
'Layer_023': {'p': 0.501816987002085, 'type': 'Dropout'},
'Layer_024': {'input': 'COMPUTE', 'output': 2317, 'type': 'Linear'},
'Layer_025': {'type': 'ReLU'},
'Layer_026': {'input': 2317, 'output': 'COMPUTE', 'type': 'Linear'},
'Layer_027': {'input': 3018, 'output': 3888, 'type': 'Linear'},
'Layer_028': {'type': 'ReLU'},
'Layer_029': {'input': 3888, 'output': 'COMPUTE', 'type': 'Linear'}
I think my error come from 'Layer_022': {'output': 'COMPUTE', 'type': 'AdaptiveAvgPool2d'} or from this on 'Layer_024': {'input': 'COMPUTE', 'output': 2317, 'type': 'Linear'} but I'm not sure... I mean I don't really now how to compute theses values, and that's why I'm asking for some help :)
I already try to put 519 on the output of 'Layer_022': {'output': 'COMPUTE', 'type': 'AdaptiveAvgPool2d'}, I tried also different values likes (2) (2,2)...

You need to put a nn.Flatten() in there. You've created a flatten layer in your code, but you need to put it in like the others. A similar method would be to call x = self.Layer_023(x).view([x.shape[0],-1]) in your forward call in order to get a size of [batch x feats].
For example:
In [3]: a = torch.randn([16,200,1,1])
In [4]: b = torch.nn.Linear(200,100)
In [5]: b(a)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (3200x1 and 200x100)
In [6]: b(a.view([a.shape[0],-1]))
Out[6]:
tensor([[ 1.2927, -0.0799, 0.3909, ..., 0.5051, 0.4727, -0.1759],
[-0.2969, 0.2622, 0.6283, ..., -0.8404, -0.7275, -0.2853],
[ 0.3116, 0.2436, -1.0069, ..., 1.9674, -0.3689, -0.1099],
...,
[-0.6393, 0.3817, 0.0246, ..., 0.1511, -0.9695, 0.6455],
[ 0.0390, -0.7878, 0.3007, ..., 0.8577, -0.2808, -0.2726],
[ 0.1561, 0.0472, -0.0222, ..., 0.9957, -0.4121, -0.1465]],
grad_fn=<AddmmBackward0>)

Related

TimeDistributedImageDataGenerator for many-to-one segmentation problem

I'm trying to implement this segmentation problem.
[https://user-images.githubusercontent.com/91024790/178153192-040ab44c-7b9f-4cfd-8e11-a3cdca2070e9.png][1]
My dataset is composed by images and masks.
In order to create a sequences of images and fed them into the network I used TimeDistributedImageDataGenerator ( https://github.com/kivijoshi/TimeDistributedImageDataGenerator/blob/master/TimeDistributedImageDataGenerator/TimeDistributedImageDataGenerator.py)
Here attached my code:
'''
seed=42
from keras.preprocessing.image import ImageDataGenerator
img_data_gen_args = dict(rescale=1./255,
rotation_range=90,
zoom_range=0.2,
brightness_range=[0.3,0.9],
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.5,
time_steps=3,
horizontal_flip=True,
vertical_flip=True,
fill_mode='constant')
mask_data_gen_args = dict(
rotation_range=90,
zoom_range=0.2,
brightness_range=[0.3,0.9],
width_shift_range=0.3,
height_shift_range=0.3,
shear_range=0.5,
time_steps=1,
horizontal_flip=True,
vertical_flip=True,
fill_mode='constant',
preprocessing_function = lambda x: np.where(x>0, 1, 0).astype(x.dtype)
) #Binarize the output again.
image_data_generator = TimeDistributedImageDataGenerator(**img_data_gen_args)
mask_data_generator = TimeDistributedImageDataGenerator(**mask_data_gen_args)
image_generator = image_data_generator.flow_from_directory(train_img_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale',
target_size=(256,256),
class_mode=None) #Very important to set this otherwise it returns multiple numpy arrays
#thinking class mode is binary.
mask_generator = mask_data_generator.flow_from_directory(train_mask_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale',
target_size=(256,256) , #Read masks in grayscale
class_mode=None)
valid_img_generator = image_data_generator.flow_from_directory(val_img_path,
seed=seed,
batch_size=batch_size,
color_mode = 'grayscale',
target_size=(256,256),
class_mode=None) #Default batch size 32, if not specified here
valid_mask_generator = mask_data_generator.flow_from_directory(val_mask_path,
seed=seed,
batch_size=batch_size,
target_size=(256,256),
color_mode = 'grayscale', #Read masks in grayscale
class_mode=None) #Default batch size 32, if not specified here
train_generator = zip(image_generator, mask_generator)
val_generator = zip(valid_img_generator, valid_mask_generator)
I used time_steps=3 for image_generator and time_steps=1 for mask_generator since i would predict just the last image of a sequence of three images ( as the image suggest).
Now my image generator has (3,3,256,256,1) as shape while mask generator (3,1,256,256,1) where the first dimension is the batch size the second one the time_steps and the last three are width, height and channels.
Then i built my segmentation model:
input_l = layers.Input(shape=(input_shape))
x = (layers.TimeDistributed(layers.Conv2D( 64, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu',kernel_initializer='he_normal' ) )) (input_l)
conv2 = layers.TimeDistributed( layers.Conv2D( 64, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu' ,kernel_initializer='he_normal' ) ) (x)
x=layers.TimeDistributed(layers.MaxPooling2D(pool_size=(2,2)))(conv2)
x = layers.TimeDistributed( layers.Conv2D( 128, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu' ,kernel_initializer='he_normal' ) ) (x)
conv5 = layers.TimeDistributed( layers.Conv2D( 128, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu' ,kernel_initializer='he_normal') ) (x)
x=layers.TimeDistributed(layers.MaxPooling2D(pool_size=(2,2)))(conv5)
x = layers.TimeDistributed( layers.Conv2D( 256, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu' ,kernel_initializer='he_normal' ) ) (x)
conv8 = layers.TimeDistributed( layers.Conv2D( 256, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu',kernel_initializer='he_normal' ) ) (x)
x=layers.TimeDistributed(layers.MaxPooling2D(pool_size=(2,2)))(conv8)
x=layers.Bidirectional(layers.ConvLSTM2D(256,kernel_size=(3,3),padding='same',strides=(1,1),return_sequences=True,recurrent_dropout=0.2))(x)
up1 = layers.TimeDistributed( layers.Conv2DTranspose( 512,kernel_size=(3,3),padding='same',strides=(2,2)))(x)
concat1 = layers.concatenate([up1, conv8])
x = layers.TimeDistributed( layers.Conv2D( 256, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu' ,kernel_initializer='he_normal' ) ) (concat1)
x = layers.TimeDistributed( layers.Conv2D( 256, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu' ,kernel_initializer='he_normal' ) ) (x)
up2 = layers.TimeDistributed( layers.Conv2DTranspose( 256,kernel_size=(3,3),padding='same',strides=(2,2)))(x)
concat2 = layers.concatenate([up2, conv5])
x = layers.TimeDistributed( layers.Conv2D( 128, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu',kernel_initializer='he_normal' ) ) (concat2)
x = layers.TimeDistributed( layers.Conv2D( 128, kernel_size=(3, 3),padding='same',strides=(1,1) ,activation='relu',kernel_initializer='he_normal' ) ) (x)
up3 = layers.TimeDistributed( layers.Conv2DTranspose( 128,kernel_size=(3,3),padding='same',strides=(2,2)))(x)
concat3 = layers.concatenate([up3, conv2])
x = layers.TimeDistributed( layers.Conv2D( 64, kernel_size=(3, 3),padding='same',strides=(1,1),activation='relu' ,kernel_initializer='he_normal' ) ) (concat3)
x=layers.Bidirectional(layers.ConvLSTM2D(32,kernel_size=(3,3),padding='same',strides=(1,1),return_sequences=False,recurrent_dropout=0.2))(x)
x=tf.expand_dims(x,axis=1)
out = layers.Conv2D( 1, kernel_size=(1, 1),padding='same',strides=(1,1), activation='sigmoid' ) (x)
model = models.Model(inputs=input_l, outputs=out)
model.summary()
Model: "model_1"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
input_2 (InputLayer) [(None, 3, 256, 256 0 []
, 1)]
time_distributed_17 (TimeDistr (None, 3, 256, 256, 640 ['input_2[0][0]']
ibuted) 64)
time_distributed_18 (TimeDistr (None, 3, 256, 256, 36928 ['time_distributed_17[0][0]']
ibuted) 64)
time_distributed_19 (TimeDistr (None, 3, 128, 128, 0 ['time_distributed_18[0][0]']
ibuted) 64)
time_distributed_20 (TimeDistr (None, 3, 128, 128, 73856 ['time_distributed_19[0][0]']
ibuted) 128)
time_distributed_21 (TimeDistr (None, 3, 128, 128, 147584 ['time_distributed_20[0][0]']
ibuted) 128)
time_distributed_22 (TimeDistr (None, 3, 64, 64, 1 0 ['time_distributed_21[0][0]']
ibuted) 28)
time_distributed_23 (TimeDistr (None, 3, 64, 64, 2 295168 ['time_distributed_22[0][0]']
ibuted) 56)
time_distributed_24 (TimeDistr (None, 3, 64, 64, 2 590080 ['time_distributed_23[0][0]']
ibuted) 56)
time_distributed_25 (TimeDistr (None, 3, 32, 32, 2 0 ['time_distributed_24[0][0]']
ibuted) 56)
bidirectional_2 (Bidirectional (None, 3, 32, 32, 5 9439232 ['time_distributed_25[0][0]']
) 12)
time_distributed_26 (TimeDistr (None, 3, 64, 64, 5 2359808 ['bidirectional_2[0][0]']
ibuted) 12)
concatenate_3 (Concatenate) (None, 3, 64, 64, 7 0 ['time_distributed_26[0][0]',
68) 'time_distributed_24[0][0]']
time_distributed_27 (TimeDistr (None, 3, 64, 64, 2 1769728 ['concatenate_3[0][0]']
ibuted) 56)
time_distributed_28 (TimeDistr (None, 3, 64, 64, 2 590080 ['time_distributed_27[0][0]']
ibuted) 56)
time_distributed_29 (TimeDistr (None, 3, 128, 128, 590080 ['time_distributed_28[0][0]']
ibuted) 256)
concatenate_4 (Concatenate) (None, 3, 128, 128, 0 ['time_distributed_29[0][0]',
384) 'time_distributed_21[0][0]']
time_distributed_30 (TimeDistr (None, 3, 128, 128, 442496 ['concatenate_4[0][0]']
ibuted) 128)
time_distributed_31 (TimeDistr (None, 3, 128, 128, 147584 ['time_distributed_30[0][0]']
ibuted) 128)
time_distributed_32 (TimeDistr (None, 3, 256, 256, 147584 ['time_distributed_31[0][0]']
ibuted) 128)
concatenate_5 (Concatenate) (None, 3, 256, 256, 0 ['time_distributed_32[0][0]',
192) 'time_distributed_18[0][0]']
time_distributed_33 (TimeDistr (None, 3, 256, 256, 110656 ['concatenate_5[0][0]']
ibuted) 64)
bidirectional_3 (Bidirectional (None, 256, 256, 64 221440 ['time_distributed_33[0][0]']
) )
tf.expand_dims_1 (TFOpLambda) (None, 1, 256, 256, 0 ['bidirectional_3[0][0]']
64)
conv2d_23 (Conv2D) (None, 1, 256, 256, 65 ['tf.expand_dims_1[0][0]']
1)
==================================================================================================
Total params: 16,963,009
Trainable params: 16,963,009
Non-trainable params: 0
Everything works,however dice coefficient is very low. I think that the main problem is a mismatch between masks and images.
it is possible that not having the same length of sequences the images and masks do not match
Is it possible that having different lenght of sequences results in a mismatch between images and masks? Any ideas? Thank you in advance

PyTorch - RuntimeError: Sizes of tensors must match except in dimension 2. Got 55 and 54 (The offending index is 0)

I used a 3DUnet with resblock to segment a CT image with input torch size of [1, 1, 96, 176, 176], but it throws the following error:
RuntimeError: Sizes of tensors must match except in dimension 2. Got 55 and 54 (The offending index is 0)
Hence I traced back, I found the error comes from
outputs = self.decoder_stage2(torch.cat([short_range6, long_range3], dim=1)) + short_range6
The short_range6 has torch.Size([1, 64, 24, 55, 40]) while the long_range3 has torch.Size([1, 128, 24, 54, 40]). I think this is because something not being a power of 2, but cannot find where to modify.
Below is the complete structure of the network, really thanks for any help!
class ResUNet(nn.Module):
def __init__(self, in_channel=1, out_channel=2 ,training=True):
super().__init__()
self.training = training
self.dorp_rate = 0.2
self.encoder_stage1 = nn.Sequential(
nn.Conv3d(in_channel, 16, 3, 1, padding=1),
nn.PReLU(16),
nn.Conv3d(16, 16, 3, 1, padding=1),
nn.PReLU(16),
)
self.encoder_stage2 = nn.Sequential(
nn.Conv3d(32, 32, 3, 1, padding=1),
nn.PReLU(32),
nn.Conv3d(32, 32, 3, 1, padding=1),
nn.PReLU(32),
nn.Conv3d(32, 32, 3, 1, padding=1),
nn.PReLU(32),
)
self.encoder_stage3 = nn.Sequential(
nn.Conv3d(64, 64, 3, 1, padding=1),
nn.PReLU(64),
nn.Conv3d(64, 64, 3, 1, padding=2, dilation=2),
nn.PReLU(64),
nn.Conv3d(64, 64, 3, 1, padding=4, dilation=4),
nn.PReLU(64),
)
self.encoder_stage4 = nn.Sequential(
nn.Conv3d(128, 128, 3, 1, padding=3, dilation=3),
nn.PReLU(128),
nn.Conv3d(128, 128, 3, 1, padding=4, dilation=4),
nn.PReLU(128),
nn.Conv3d(128, 128, 3, 1, padding=5, dilation=5),
nn.PReLU(128),
)
self.decoder_stage1 = nn.Sequential(
nn.Conv3d(128, 256, 3, 1, padding=1),
nn.PReLU(256),
nn.Conv3d(256, 256, 3, 1, padding=1),
nn.PReLU(256),
nn.Conv3d(256, 256, 3, 1, padding=1),
nn.PReLU(256),
)
self.decoder_stage2 = nn.Sequential(
nn.Conv3d(128 + 64, 128, 3, 1, padding=1),
nn.PReLU(128),
nn.Conv3d(128, 128, 3, 1, padding=1),
nn.PReLU(128),
nn.Conv3d(128, 128, 3, 1, padding=1),
nn.PReLU(128),
)
self.decoder_stage3 = nn.Sequential(
nn.Conv3d(64 + 32, 64, 3, 1, padding=1),
nn.PReLU(64),
nn.Conv3d(64, 64, 3, 1, padding=1),
nn.PReLU(64),
nn.Conv3d(64, 64, 3, 1, padding=1),
nn.PReLU(64),
)
self.decoder_stage4 = nn.Sequential(
nn.Conv3d(32 + 16, 32, 3, 1, padding=1),
nn.PReLU(32),
nn.Conv3d(32, 32, 3, 1, padding=1),
nn.PReLU(32),
)
self.down_conv1 = nn.Sequential(
nn.Conv3d(16, 32, 2, 2),
nn.PReLU(32)
)
self.down_conv2 = nn.Sequential(
nn.Conv3d(32, 64, 2, 2),
nn.PReLU(64)
)
self.down_conv3 = nn.Sequential(
nn.Conv3d(64, 128, 2, 2),
nn.PReLU(128)
)
self.down_conv4 = nn.Sequential(
nn.Conv3d(128, 256, 3, 1, padding=1),
nn.PReLU(256)
)
self.up_conv2 = nn.Sequential(
nn.ConvTranspose3d(256, 128, 2, 2),
nn.PReLU(128)
)
self.up_conv3 = nn.Sequential(
nn.ConvTranspose3d(128, 64, 2, 2),
nn.PReLU(64)
)
self.up_conv4 = nn.Sequential(
nn.ConvTranspose3d(64, 32, 2, 2),
nn.PReLU(32)
)
# 256*256
self.map4 = nn.Sequential(
nn.Conv3d(32, out_channel, 1, 1),
nn.Upsample(scale_factor=(1, 1, 1), mode='trilinear', align_corners=False),
nn.Softmax(dim=1)
)
# 128*128
self.map3 = nn.Sequential(
nn.Conv3d(64, out_channel, 1, 1),
nn.Upsample(scale_factor=(2, 2, 2), mode='trilinear', align_corners=False),
nn.Softmax(dim=1)
)
# 64*64
self.map2 = nn.Sequential(
nn.Conv3d(128, out_channel, 1, 1),
nn.Upsample(scale_factor=(4, 4, 4), mode='trilinear', align_corners=False),
nn.Softmax(dim=1)
)
# 32*32
self.map1 = nn.Sequential(
nn.Conv3d(256, out_channel, 1, 1),
nn.Upsample(scale_factor=(8, 8, 8), mode='trilinear', align_corners=False),
nn.Softmax(dim=1)
)
def forward(self, inputs):
long_range1 = self.encoder_stage1(inputs) + inputs
short_range1 = self.down_conv1(long_range1)
long_range2 = self.encoder_stage2(short_range1) + short_range1
long_range2 = F.dropout(long_range2, self.dorp_rate, self.training)
short_range2 = self.down_conv2(long_range2)
long_range3 = self.encoder_stage3(short_range2) + short_range2
long_range3 = F.dropout(long_range3, self.dorp_rate, self.training)
short_range3 = self.down_conv3(long_range3)
long_range4 = self.encoder_stage4(short_range3) + short_range3
long_range4 = F.dropout(long_range4, self.dorp_rate, self.training)
short_range4 = self.down_conv4(long_range4)
outputs = self.decoder_stage1(long_range4) + short_range4
outputs = F.dropout(outputs, self.dorp_rate, self.training)
output1 = self.map1(outputs)
short_range6 = self.up_conv2(outputs)
outputs = self.decoder_stage2(torch.cat([short_range6, long_range3], dim=1)) + short_range6
outputs = F.dropout(outputs, self.dorp_rate, self.training)
output2 = self.map2(outputs)
short_range7 = self.up_conv3(outputs)
outputs = self.decoder_stage3(torch.cat([short_range7, long_range2], dim=1)) + short_range7
outputs = F.dropout(outputs, self.dorp_rate, self.training)
output3 = self.map3(outputs)
short_range8 = self.up_conv4(outputs)
outputs = self.decoder_stage4(torch.cat([short_range8, long_range1], dim=1)) + short_range8
output4 = self.map4(outputs)
if self.training is True:
return output1, output2, output3, output4
else:
return output4```
You can pad your image's dimensions to be multiple of 32's. By doing this, you won't have to change the 3DUnet's parameters.
I will provide you a simple code to show you the way.
# I assume that you named your input image as img
padding1_mult = math.floor(img.shape[3] / 32) + 1
padding2_mult = math.floor(img.shape[4] / 32) + 1
pad1 = (32 * padding1_mult) - img.shape[3]
pad2 = (32 * padding2_mult) - img.shape[4]
padding = nn.ReplicationPad2d((0, pad2, pad1, 0, 0 ,0))
img = padding(img)
After this operation, your image shape must be torch.Size([1, 1, 96, 192, 192])

flatten_json is giving me a long list of columns and a single row instead of the required dataframe structure

I am converting a nested json file having more than 100 records into a flattend csv file. The sample json file is shown below:
sampleJson = {
'record1':
{
'text':[ ['A', 'fried', 'is', 'a', 'nice', 'companion', '.'],
['The', 'birds', 'are', 'flying', '.']],
'values':[ [0, 1, 0, 0],
[1, 1, 0, 1]],
'pairs':[ [0, 2],
[2, 1]]
},
'record2':
{
'text':[ ['We', 'can', 'work', 'hard', 'together', '.'],
['Let', 'the', 'things', 'happen', '.'],
['There', 'is', 'always', 'a', 'way', 'out', '.']],
'values':[ [0, 1, 0, 0],
[0, 1, 1, 1],
[1, 1, 0, 1]],
'pairs':[ [0, 2],
[3, 4],
[2, 1]]
},
..... 100 records
}
The csv structure i want from this nested json is:
record1, A friend is a nice companion., 0, 1, 0, 0, [0, 2]
, The bids are flying., 1, 1, 0, 1, [2, 1]
record2, We can work hard together., 0, 1, 0, 0, [0, 2]
, Let the things happen., 0, 1, 1, 1, [4, 3]
, There is always a way out., 1, 1, 0, 1, [2, 1]
record3,
....... upto 100 records
I used the following code to flatten the nested file:
def flatten_json(y):
out = {}
def flatten(x, name=''):
if type(x) is dict:
for a in x:
flatten(x[a], name + a + '_')
elif type(x) is list:
i = 0
for a in x:
flatten(a, name + str(i) + '_')
i += 1
else:
out[name[:-1]] = x
flatten(y)
return out
flatIt = flatten_json(sampleJson)
df= pd.json_normalize(flatIt)
df.to_csv('outPutFile.csv', encoding='utf-8')
print(df)
I am getting a long list of columns with a structure like record1.text, record1.values, record1.pairs, record2.text and so on with one row and also each word of the sentences in the text is in a separate column.
I will appreciate some help.
Thanks..
You can use this example to parse the Json to dataframe:
import pandas as pd
sampleJson = {
'record1':
{
'text':[ ['A', 'fried', 'is', 'a', 'nice', 'companion', '.'],
['The', 'birds', 'are', 'flying', '.']],
'values':[ [0, 1, 0, 0],
[1, 1, 0, 1]],
'pairs':[ [0, 2],
[2, 1]]
},
'record2':
{
'text':[ ['We', 'can', 'work', 'hard', 'together', '.'],
['Let', 'the', 'things', 'happen', '.'],
['There', 'is', 'always', 'a', 'way', 'out', '.']],
'values':[ [0, 1, 0, 0],
[0, 1, 1, 1],
[1, 1, 0, 1]],
'pairs':[ [0, 2],
[3, 4],
[2, 1]]
},
}
all_data = []
for k, v in sampleJson.items():
texts, values, pairs = v['text'], v['values'], v['pairs']
for t, val, p in zip(texts, values, pairs):
all_data.append({
'record': k,
'text': ' '.join(t),
'pairs': p,
**{'val_{}'.format(i): val_ for i, val_ in enumerate(val, 1)}
})
df = pd.DataFrame(all_data)
print(df)
Prints this dataframe:
record text pairs val_1 val_2 val_3 val_4
0 record1 A fried is a nice companion . [0, 2] 0 1 0 0
1 record1 The birds are flying . [2, 1] 1 1 0 1
2 record2 We can work hard together . [0, 2] 0 1 0 0
3 record2 Let the things happen . [3, 4] 0 1 1 1
4 record2 There is always a way out . [2, 1] 1 1 0 1

Producing different p value each time I 'Knit HTML'

I have embedded some code in my Rmd file to perform t.test on a set of data. But weirdly, each time I click on 'Knit HTML', I see different outputs for p-value in the HTML file. But the same doesn't happen if I am running code in the console. Can somebody please help understand why it must be happening and how I can avoid it?
Below is that piece of code:
```{r, echo=TRUE}
tresults <- matrix(nrow = 4, ncol = 3)
for (i in 1:4){
options(scipen = 999) #Force are to not use exponential notation
tresult <- t.test(years[[i]][,11], years[[i]][,21], var.equal = T, alternative = "greater")
tresults[i,] <- as.numeric(c(tresult[[1]], tresult[[2]], tresult[[3]]))}
tresults <- format(round(tresults, 3), nsmall = 3) #round off to 3 decimal points
tresults <- cbind(c("2004-05", "2005-06", "2006-07", "2007-08"), tresults)
colnames(tresults) <- c("years", "t", "df", "p-value")
print(tresults)
```
Edit:
'years' is basically a list that contains data-frame corresponding to each year -
#List the datasets
years <- list(prod_data_0405, prod_data_0506, prod_data_0607, prod_data_0708)
I have created a sample of this list, converted it to data-frame for the convenience of sharing using following command:
years_sample <- as.data.frame(do.call(rbind, years_sample))
and saved it on this link. Please use it to test the code and let me know.
Edit 2
Here is the sample that I have created using dput(years)
list(structure(list(ID = c(1, 2, 3, 4, 5, 6), Area..acres. = c(4,
1, 2, 3, 1, 1), Cotton = c(2, 2.5, 2, 5, 3, 0), Pigeon.pea = c(0.33,
NaN, 0.5, 0.21, NaN, 0), Soyabean = c(4, NaN, NaN, 6, NaN, NaN
), Sorghum = c("", "", "", "6", "", ""), Other = c(NaN, NaN,
NaN, 1.6, NaN, NaN), Total.yield..Quintal. = c(6.33, 2.5, 5,
23.56, 3, 0), Gross.Income..Rs. = c(4695, 4750, 4612.5, 11531.67,
5700, 3800), Total.Expenditure..Rs. = c(1955, 1700, 2237, 3296.67,
1520, 2900), Net.Income..Rs. = c(2740, 3050, 2366.5, 235, 4180,
0), Area..acres..1 = c(1, 8, 2, 3, 5, 5), Cotton.1 = c("", "4.6",
"5", "", "2.33", "0"), Pigeon.pea.1 = c(NaN, 0.4, 0.5, 0.33,
0.2, 0), Soyabean.1 = c(NaN, 2, NaN, 0.23, 3, NaN), Sorghum.1 = c(4,
11, NaN, NaN, 4.5, 0), Other.1 = c(NaN, NaN, NaN, NaN, NaN, NaN
), Total.yield..Quintal..1 = c(4, 40, 11, 1.7, 15.1, 0), Gross.Income..Rs..1 = c(3200,
7603.12, 7612.5, 1013.33, 4705, 5980), Total.Expenditure..Rs..1 = c(1950,
3042.7, 2850, 1193.33, 2060, 4050), Net.Income..Rs..1 = c(1250,
4560.72, 4762.5, -150, 2645, 2852.5)), .Names = c("ID", "Area..acres.",
"Cotton", "Pigeon.pea", "Soyabean", "Sorghum", "Other", "Total.yield..Quintal.",
"Gross.Income..Rs.", "Total.Expenditure..Rs.", "Net.Income..Rs.",
"Area..acres..1", "Cotton.1", "Pigeon.pea.1", "Soyabean.1", "Sorghum.1",
"Other.1", "Total.yield..Quintal..1", "Gross.Income..Rs..1",
"Total.Expenditure..Rs..1", "Net.Income..Rs..1"), row.names = c(NA,
6L), class = "data.frame"), structure(list(ID = c(1, 2, 3, 4,
5, 6), Area..acres. = c(2, 1, 2, 6, 1, 1), Cotton = c(NaN, 6,
2, 1.75, NaN, NaN), Pigeon.pea = c(0.75, 2, NaN, 1, NaN, NaN),
Soyabean = c(4, NaN, 1.5, 2.75, 3, 3), Sorghum = c(1, NaN,
2, 1, NaN, NaN), Other = c(0.25, 0.1, NaN, NaN, NaN, NaN),
Total.yield..Quintal. = c(12, 8.1, 11, 18.5, 3, 3), Gross.Income..Rs. = c(6525,
3000, 5575, 4666.67, 3240, 3800), Total.Expenditure..Rs. = c(3785,
2290, 2270, 2450, 1950, 2900), Net.Income..Rs. = c(2740,
710, 3305, 2216.67, 1290, 900), Area..acres..1 = c(3, 2,
2, 5, 4, 5), Cotton.1 = c(2, 2.5, 2, NaN, 0.83, 3), Pigeon.pea.1 = c(0.75,
0.25, 1, NaN, 1, 0.62), Soyabean.1 = c("", "", "", "", "",
""), Sorghum.1 = c(4, 0.1, 4, 1.5, 2, 3), Other.1 = c(NaN,
0.1, NaN, NaN, NaN, NaN), Total.yield..Quintal..1 = c(9.5,
5.7, 7, 3, 7.5, 17.5), Gross.Income..Rs..1 = c(4433.33, 3500,
4275, 1275, 2900, 5980), Total.Expenditure..Rs..1 = c(3823.33,
3270, 2660, 2075, 2262.5, 3560), Net.Income..Rs..1 = c(610,
230, 1615, 800, 637.5, 2420)), .Names = c("ID", "Area..acres.",
"Cotton", "Pigeon.pea", "Soyabean", "Sorghum", "Other", "Total.yield..Quintal.",
"Gross.Income..Rs.", "Total.Expenditure..Rs.", "Net.Income..Rs.",
"Area..acres..1", "Cotton.1", "Pigeon.pea.1", "Soyabean.1", "Sorghum.1",
"Other.1", "Total.yield..Quintal..1", "Gross.Income..Rs..1",
"Total.Expenditure..Rs..1", "Net.Income..Rs..1"), row.names = c(NA,
6L), class = "data.frame"), structure(list(ID = c(1, 2, 3, 4,
5, 6), Area..acres. = c(2, 1.5, 2, 3, 2, 3), Cotton = c(NaN,
2, NaN, 2.66, NaN, NaN), Pigeon.pea = c(NaN, 0.33, 0.4, 0.53,
0.5, NaN), Soyabean = c(3.5, NaN, 2, NaN, 3, 2.66), Sorghum = c(NaN,
NaN, NaN, 0.25, 2, NaN), Other = c(NaN, NaN, NaN, 0.19, NaN,
NaN), Total.yield..Quintal. = c(NaN, 3.5, 4.8, 10.02, 11, 8),
Gross.Income..Rs. = c(4200, 4646.66, 5389, 6507.33, 6600,
3200), Total.Expenditure..Rs. = c("1670", "2060", "2385",
"2528.33", "3006.5", "1426.66"), Net.Income..Rs. = c(2530,
2586.66, 3004, 3979, 3592.5, 1773.34), Area..acres..1 = c(2,
1.5, 2, 7, 1.5, 7.5), Cotton.1 = c(2, 3.33, 3, 2.16, 2, 0.93
), Pigeon.pea.1 = c(1, 0.33, 0.4, 0.33, 0.26, 0.2), Soyabean.1 = c(NaN,
NaN, NaN, NaN, NaN, NaN), Sorghum.1 = c(NaN, NaN, NaN, 5,
NaN, 6), Other.1 = c(NaN, NaN, NaN, NaN, NaN, NaN), Total.yield..Quintal..1 = c(6,
5.5, 6.8, 20, 3.4, 14.5), Gross.Income..Rs..1 = c(5930, 7500,
6920, 5100, 4666, 2822.66), Total.Expenditure..Rs..1 = c(3225,
3400, 3610, 3654.28, 5600, 1754.66), Net.Income..Rs..1 = c(2705,
4100, 3310, 1445.72, -934, 1068)), .Names = c("ID", "Area..acres.",
"Cotton", "Pigeon.pea", "Soyabean", "Sorghum", "Other", "Total.yield..Quintal.",
"Gross.Income..Rs.", "Total.Expenditure..Rs.", "Net.Income..Rs.",
"Area..acres..1", "Cotton.1", "Pigeon.pea.1", "Soyabean.1", "Sorghum.1",
"Other.1", "Total.yield..Quintal..1", "Gross.Income..Rs..1",
"Total.Expenditure..Rs..1", "Net.Income..Rs..1"), row.names = c(NA,
6L), class = "data.frame"), structure(list(ID = c(1, 2, 3, 4,
5, 6), Area..acres. = c(2, 2, 2, 3, 2, 1), Cotton = c(NaN, 3,
2, NaN, NaN, 4), Pigeon.pea = c(NaN, NaN, 0.5, 0.5, 1.5, 1),
Soyabean = c(3.66, NaN, NaN, 3, 1, NaN), Sorghum = c(2, NaN,
NaN, 1, 2.5, NaN), Other = c(22, NaN, NaN, 0.17, NaN, NaN
), Total.yield..Quintal. = c(12.3, 6, 5, 14.3, 10, 5), Gross.Income..Rs. = c(6030,
6420, 5562, 8183.33, 7780, 11800), Total.Expenditure..Rs. = c(3192,
4080, 3350, 20530, 3240, 5130), Net.Income..Rs. = c(2838,
2340, 2212, 5653.33, 4540, 6670), Area..acres..1 = c(2, 1,
2, 8, 1, 5), Cotton.1 = c(2, 4, 2.5, 3, 5.8, 5), Pigeon.pea.1 = c(3,
NaN, 0.5, 0.7, NaN, 0.25), Soyabean.1 = c(1, NaN, NaN, NaN,
NaN, NaN), Sorghum.1 = c(NaN, NaN, NaN, 3.7, NaN, 2), Other.1 = c(NaN,
NaN, NaN, NaN, NaN, NaN), Total.yield..Quintal..1 = c(0,
4, 6, 28, 5.8, 23), Gross.Income..Rs..1 = c(8675, 9760, 6677.5,
7417.7, 13050, 10860), Total.Expenditure..Rs..1 = c(7700,
6750, 5425, 4112.5, 6300, 4870), Net.Income..Rs..1 = c(975,
3010, 1252.5, 3305.7, 4750, 5990)), .Names = c("ID", "Area..acres.",
"Cotton", "Pigeon.pea", "Soyabean", "Sorghum", "Other", "Total.yield..Quintal.",
"Gross.Income..Rs.", "Total.Expenditure..Rs.", "Net.Income..Rs.",
"Area..acres..1", "Cotton.1", "Pigeon.pea.1", "Soyabean.1", "Sorghum.1",
"Other.1", "Total.yield..Quintal..1", "Gross.Income..Rs..1",
"Total.Expenditure..Rs..1", "Net.Income..Rs..1"), row.names = c(NA,
6L), class = "data.frame"))

HighCharts - Filling a heatmap from SQL Query

Im trying to fill a HighCharts Heatmap with data returned from an SQL Query.
What i have in the JS file is
$(function () {
var chart;
$(document).ready(function() {
$.getJSON("php/all-counties-sales-data-box.php", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'chart-box-combined',
type: 'heatmap',
marginTop: 40,
marginBottom: 80,
plotBorderWidth: 1
},
title: {
text: 'Sales per employee per weekday'
},
xAxis: {
categories: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
},
yAxis: {
categories: ['Lucozade', 'Rockstar', 'Sprite', 'Monster', '7Up', 'Fanta', 'Coke'],
title: null
},
colorAxis: {
min: 0,
minColor: '#FFFFFF',
maxColor: Highcharts.getOptions().colors[0]
},
legend: {
align: 'right',
layout: 'vertical',
margin: 0,
verticalAlign: 'top',
y: 25,
symbolHeight: 280
},
tooltip: {
formatter: function () {
return '<b>' + this.series.xAxis.categories[this.point.x] + '</b> sold <br><b>' +
this.point.value + '</b> items on <br><b>' + this.series.yAxis.categories[this.point.y] + '</b>';
}
},
series: [{
name: 'Sales per Shell',
borderWidth: 1,
data:
[[0, 0, 10], [0, 1, 19], [0, 2, 8], [0, 3, 24], [0, 4, 67], [0, 5, 67], [0, 6, 67],
[1, 0, 92], [1, 1, 58], [1, 2, 78], [1, 3, 117], [1, 4, 48], [1, 5, 48], [1, 6, 48],
[2, 0, 35], [2, 1, 15], [2, 2, 123], [2, 3, 64], [2, 4, 52],
[3, 0, 72], [3, 1, 132], [3, 2, 114], [3, 3, 19], [3, 4, 16],
[4, 0, 38], [4, 1, 5], [4, 2, 8], [4, 3, 117], [4, 4, 115],
[5, 0, 88], [5, 1, 32], [5, 2, 12], [5, 3, 6], [5, 4, 120],
[6, 0, 13], [6, 1, 44], [6, 2, 88], [6, 3, 98], [6, 4, 96],
[7, 0, 31], [7, 1, 1], [7, 2, 82], [7, 3, 32], [7, 4, 30],
[8, 0, 85], [8, 1, 97], [8, 2, 123], [8, 3, 64], [8, 4, 84],
[9, 0, 47], [9, 1, 114], [9, 2, 31], [9, 3, 48], [9, 4, 91],
[10, 0, 47],
[11, 0, 47],
],
dataLabels: {
enabled: true,
color: '#000000'
}
}]
});
});
});
});
And what im trying to fill it with is data from the query
$sth = mysql_query("Select SUM(Profit) as profitSum From FactSales GROUP BY ShellType, SaleMonth");
$rows1 = array();
$rows1['profit'] = 'profitSum';
while($rr = mysql_fetch_assoc($sth)) {
$rows1['series'][] = $rr['profitSum'];
}
$result = array();
array_push($result,$rows1);
What do i actually need to change for the "series" data to be filled with the data returned from the sql query?
Heres the JSON response as requested
[{"profit":"profitSum","data":[1329230,1796743,1789417,1327813,1457103,1198845,1859826,1770589,1555410,1310369,2183499,1212897,6424306,6426002,6345153,6167415,6969392,5974880,6407699,6278843,6622002,5962102,5198177,5386392,72991,2321397,1751565,2029890,642041,1314314,1322492,1557859,1647784,1831767,1347480,1739353,1742597,1636006,1728247,1709689,1206645,1383206,1119153,1378317,1527356,1937898,1485322,1404498,1868629,1635265,1860456,1293870,1485349,2031389,1834402,1291372,1838382,1616009,781641,1421830,1763592,1279535,1123468,2024766,975863,1461843,1318585,1137336,1111721,1407705,2349652,1260858,1144070,1219659,1378615,1354139,2015115,1408858,2650864,1810850,1380157,1844909,2055306,1913532,1701963]}]