<jupyter_start><jupyter_text>PCA<jupyter_code>EXP_NO = '044'
ANGLE_NO = '090'
gait_key_poses = 16
image_vec_shape = 12
conditional_vec_shape = 4
full_vec_shape = image_vec_shape+conditional_vec_shape
labels = ['nm-01', 'nm-02', 'nm-03', 'nm-04', 'nm-05', 'nm-06']
labels = ['nm-01', 'nm-02', 'nm-03', 'nm-04', 'nm-05', 'nm-06']
batch_imgs = [None for _ in labels]
pro_imgs = [None for _ in labels]
names = [None for _ in labels]
for i, label in enumerate(labels):
    batch_imgs[i] = []
    names[i] = []
    for file in os.listdir('/'.join([
                                os.getcwd(),
                                'GaitDatasetB-silh',
                                EXP_NO,
                                label,
                                ANGLE_NO
                                ])):
        if file[-3:]!='png':
            continue
        
        batch_imgs[i].append(
                    gait.preprocess(
                        cv2.imread('/'.join([
                                            os.getcwd(),
                                            'GaitDatasetB-silh',
                                            EXP_NO,
                                            label,
                                            ANGLE_NO,
                                            file
                                        ])
                                   )
                                )
                            )
        names[i].append(
            '/'.join([
                     os.getcwd(),
                     'GaitDatasetB-silh',
                     EXP_NO,
                     label,
                     ANGLE_NO,
                     file
                     ])
        )
    batch_imgs[i] = np.array(batch_imgs[i])
    names[i] = np.array(names[i])
        
y = []
for i in range(len(labels)):
    y.append(i*np.ones((batch_imgs[i].shape[0])))
mainImgs = np.concatenate(batch_imgs,axis=0)
y = np.concatenate(y, axis=0)
mainImgs.shape
X, _, _, _, _, info = gait.get_feature_vectors(mainImgs,preproc=lambda x:x)
np.savez_compressed('eig90',**info)<jupyter_output><empty_output><jupyter_text>DataSet Generation<jupyter_code>eig = np.load('eig90.npz',allow_pickle=True)
pr_imgs = [None for _ in labels]
names = [None for _ in labels]
pro_imgs = [None for _ in labels]


for i, label in enumerate(labels[:2]):
    _, pr_imgs[i], names[i] = gait.fetch_data(label,90,True, True, True)
    
    
key_pose_info = gait.fetch_labels(save=False, override=True)

y = []
for i in range(len(labels[:2])):
    pro_imgs[i] = np.moveaxis(np.concatenate(pr_imgs[i], axis=-1), -1, 0) 
    names[i] = np.concatenate(names[i], axis=-1)
    y.append(i*np.ones((len(pr_imgs[i],))))
datasetImgs = np.concatenate(pro_imgs[:2], axis=0)
names = np.concatenate(names[:2], axis=0)
z = np.array([key_pose_info[ele] for ele in names])
X, _, _, _, _, _ = gait.get_feature_vectors(datasetImgs,
                                            preproc=lambda x:x, 
                                            eigvec=eig['eigvec'], 
                                            eigvalue=eig['eigvalue'])<jupyter_output><empty_output><jupyter_text>Dataset Prep Convert to PEI with kmeans<jupyter_code>for subject in pr_imgs:
    for gaitCycle in subject:
        gait.get_feature_vectors(gaitCycle)
#Occlusion induction
clf = RandomForestClassifier(n_estimators=100,
                             max_depth=5,
                             min_samples_split=3
                             )
pickle.dump(clf, open('randomForest.sav','wb'))<jupyter_output><empty_output><jupyter_text>AutoEncoder<jupyter_code>from train import CVAE_FULL

CVAE_FULL.load_weights('weights/CVAE_FULL.h5')

encoder = keras.models.Model(CVAE_FULL.input, CVAE_FULL.get_layer("concat_zcond").output)
encoded_vec = gait.encode_data(encoder,label_angle='090',override=True)
key_pose_info = gait.fetch_labels(save=False, override=True)
model_BiRNN.load_weights('weights/BiRNN.h5')
labels = ['nm-01', 'nm-02', 'nm-03', 'nm-04', 'nm-05', 'nm-06']
enc_inp = [None for _ in labels]
names = [None for _ in labels]
z = [None for _ in labels]
for i, label in enumerate(labels):
    enc_inp[i] = []
    names[i] = []
    z[i] = []
    for exp in os.listdir('/'.join([
                                os.getcwd(),
                                'GaitDatasetB-silh'
                                ])):
        name_sub = []
        z_sub = []
        files = sorted(os.listdir(
                    '/'.join([
                            os.getcwd(),
                            'GaitDatasetB-silh',
                            exp,
                            label,
                            ANGLE_NO
                            ])
                        )
                    )
        if len(files) < 10:
            continue
            
        for file in files:
            
            if file[-3:]!='png':
                continue
            
            
        
            name_sub.append(
                '/'.join([
                     os.getcwd(),
                     'GaitDatasetB-silh',
                     exp,
                     label,
                     ANGLE_NO,
                     file
                    ])
                )
            z_sub.append(key_pose_info[name_sub[-1]])
            
        names[i].append(name_sub)
        z[i].append(z_sub)
        enc_inp[i].append(encoded_vec[
                       '/'.join([
                             os.getcwd(),
                             'GaitDatasetB-silh',
                             exp,
                             label,
                             ANGLE_NO
                            ])
                        ])
        
def recur(inp, final=[]):
    try:
        #print(len(inp))
        final.append(len(inp))
        for ele in inp:
            recur(ele,final)
        return final
    except:
        return 0
def fetch_PEI(enc_vec, z, labels):
    PEI_Vecs = [None for _ in labels]
    for i, label in enumerate(labels):
        PEI_Vecs[i] = []
        for j, key_poses in enumerate(z[i]):
            key_poses = np.array(key_poses)
            PEI_vec = [None for _ in range(16)]
            for k in range(16):
                PEI_vec[k] = np.nan_to_num(enc_vec[i][j][key_poses==k].mean(axis=0))
            PEI_Vecs[i].append(np.array(PEI_vec))
        PEI_Vecs[i] = np.array(PEI_Vecs[i])
    y = []
    for i in range(len(labels)):
        y.append(i*np.ones(PEI_Vecs[i].shape[0]))
    y = np.concatenate(y,axis=0)
    PEI_Vecs = np.concatenate(PEI_Vecs, axis=0)
    
    return PEI_Vecs, y
def occlude(enc_vec, labels, occluRatio=0.3):
    enc_vecs = copy.deepcopy(enc_vec)
    for i, label in enumerate(labels):
        for j in range(len(enc_vecs[i])):
            mask = np.random.random((len(enc_vecs[i][j]),1)) > occluRatio
            enc_vecs[i][j] = mask*enc_vecs[i][j]
    return enc_vecs
def filter_enc(enc_vec, model_birnn, labels):
    batch_enc = []
    for i, label in enumerate(labels):
        for j in range(len(enc_vec[i])):
            for k in range(len(enc_vec[i][j])-6 + 1):
                batch_enc.append(enc_vec[i][j][k:k+6])
    batch_enc = np.array(batch_enc)

    batch_enc = model_birnn.predict( batch_enc )
    cnt = 0

    for i, label in enumerate(labels):
        for j in range(len(enc_vec[i])):
            for k in range(len(enc_vec[i][j])-6 + 1):
                enc_vec[i][j][k:k+6] = batch_enc[cnt]
                cnt+=1
    
    return enc_vec
PEI_Vecs, y = fetch_PEI(enc_inp, z, labels)
enc_vec = occlude(enc_inp, labels, 0.0)
enc_vec = filter_enc(enc_vec, model_BiRNN, labels)
PEI_Vecs, y = fetch_PEI(enc_vec, z, labels)
model_classifier = keras.models.Sequential()
model_classifier.add(keras.layers.LSTM( 256,
                                        activation='tanh',
                                        return_sequences = True,
                                        input_shape=(gait_key_poses, full_vec_shape)
                                    )
                        )
model_classifier.add(keras.layers.LSTM( 128,
                                        activation='tanh',
                                        return_sequences=True,
                        )
                    )
model_classifier.add(keras.layers.LSTM(64,
                                       activation='tanh'
                        )
                    )
model_classifier.add(keras.layers.Dense(16,
                                        activation='tanh'
                        )
                    )
model_classifier.add(keras.layers.Dense(len(labels),
                                       activation='softmax'
                        )
                    )
model_classifier.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
plot_model(model_classifier, to_file='media/model_classifier_plot.png', show_shapes=True, show_layer_names=True)
im = Image.open('media/model_classifier_plot.png')
im
model_classifier.fit(PEI_Vecs, y,epochs=100)
model_classifier.save('weights/classifier.h5')
model_classifier.load_weights('weights/classifier.h5')<jupyter_output><empty_output><jupyter_text>Adding Occlusions<jupyter_code>import warnings
warnings.filterwarnings('ignore')
scores = []
for occlu in [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]:
    enc_i = occlude(enc_vec, labels, occlu)
    enc_i = filter_enc(enc_i, model_BiRNN, labels)
    test_PEI, y = fetch_PEI(enc_i, z, labels)
    y_pred = np.argmax(model_classifier.predict(test_PEI), axis=-1)
    acc = accuracy_score(y, y_pred)
    scores.append([occlu, acc])
for occlu, acc in scores:
    print('For '+str(100*occlu)+'% occlusion BiRNN achives '+str(100*acc)+'% accuracy')
print(""" Previous Results \n
Occlusion Degree \t(%)Rank-1 Accuracy (%)\n<10%\t92.38\n10%-20%\t85.71\n20%-30%\t73.33\n30%-40%\t62.85\n40%-50%\t40.75
""")<jupyter_output>Previous Results 

Occlusion Degree 	(%)Rank-1 Accuracy (%)
<10%	92.38
10%-20%	85.71
20%-30%	73.33
30%-40%	62.85
40%-50%	40.75
{'fraction_non_alphanumeric': 0.12521269142228006, 'fraction_numerical': 0.02242017816034431, 'mean_word_length': 1.553539483772042, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '642347', 'license': 'MIT', 'n_tokens_mistral': 3428, 'n_tokens_neox': 3211, 'n_words': 530, 'path': 'Gait_Recog.ipynb', 'repo_name': 'hex-plex/Gait-Occlusion-GAN', 'size': '9991'}