Skip to content

Instantly share code, notes, and snippets.

View sohiniroych's full-sized avatar

Sohini Roychowdhury sohiniroych

View GitHub Profile
@sohiniroych
sohiniroych / adjust_data.py
Created March 10, 2021 23:34
generate binary mask from grayscale images
def adjustData(img,mask,flag_multi_class,n_class):
if(flag_multi_class):
img /= 255
mask = mask[:,:,:,0] if(len(mask.shape) == 4) else mask[:,:,0]
new_mask = np.zeros(mask.shape + (n_class,))
for i in range(n_class):
new_mask[mask == i,i] = 1
new_mask = np.reshape(new_mask,(new_mask.shape[0],new_mask.shape[1]*new_mask.shape[2],new_mask.shape[3])) if flag_multi_class else np.reshape(new_mask,(new_mask.shape[0]*new_mask.shape[1],new_mask.shape[2]))
@sohiniroych
sohiniroych / loss_functions.py
Created March 10, 2021 23:31
loss functions for semantic segmentation
def dice_coef(y_true, y_pred, smooth=1):
intersection = keras.sum(y_true * y_pred, axis=[1,2,3])
union = keras.sum(y_true, axis=[1,2,3]) + keras.sum(y_pred, axis=[1,2,3])
return keras.mean( (2. * intersection + smooth) / (union + smooth), axis=0)
def dice_coef_loss(y_true, y_pred):
return -dice_coef(y_true, y_pred)
@sohiniroych
sohiniroych / data_prep.py
Created March 10, 2021 23:29
Preparing 3 class data
def prepare_multi_class_GT(GT_PATH, class_names, savepath, target_size=(512,512), n_class=3):
f_names = os.listdir(GT_PATH+class_names[0])
for files in f_names:
GT_im=np.zeros(np.concatenate((target_size,n_class),axis=None)) #This creates a zero array of size (512,512,3)
FG=np.zeros(target_size)
for idx,cn in enumerate(class_names):
lab=io.imread(GT_PATH+cn+files, as_gray=True)
lab = trans.resize(lab,target_size)
data_gen_args = dict(rotation_range=0.3,
width_shift_range=0.1,
height_shift_range=0.1,
shear_range=0.1,
zoom_range=[0.7,1],
horizontal_flip=True,
fill_mode='nearest')
@sohiniroych
sohiniroych / cloudbuild.yaml
Created February 17, 2021 21:45
cloudbuild.yaml
steps:
# Step 1: pull the container image if it is already built, if fails do not exit
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args:
- '-c'
- 'docker pull gcr.io/$PROJECT_ID/appcicd:latest || exit 0'
# Step 2: Create a docker image if none exists, esle load existing one
- name: 'gcr.io/cloud-builders/docker'
args:
@sohiniroych
sohiniroych / cloudbuild.yaml
Created February 17, 2021 21:42
YAML file
steps:
# pull the container image if it is already built
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args:
- '-c'
- 'docker pull gcr.io/$PROJECT_ID/appcicd:latest || exit 0'
# build the container image
- name: 'gcr.io/cloud-builders/docker'
args:
@sohiniroych
sohiniroych / Dockerfile
Created February 17, 2021 21:40
Dockerfile
FROM python:3.6-slim-buster
RUN python -m pip install --upgrade pip
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . /app
WORKDIR /app/app_files
CMD ["python", "app.py"]
if __name__=='__main__':
app.run( debug=True, host='0.0.0.0', port=int(os.environ.get('PORT', 8080)) )
label="Regression_model", linewidth=2)
@sohiniroych
sohiniroych / model.py
Created February 17, 2021 21:31
Model Fitting and visualization
#To generate a best fit model
X_range=np.zeros((50,3))
y_range=np.zeros((50,))
for i in range(3):
Xi=X[:,i]
vals=plt.hist(Xi,49)
plt.xlabel("Feature")
plt.ylabel("Frequency")
X_range[:,i]=np.transpose(vals[1])
y_range=model.predict(X_range)
from sklearn.tree import DecisionTreeRegressor
reg=DecisionTreeRegressor(max_depth=5)
reg.fit(X,y)
y_pred=reg.predict(X)
print(reg.score)