본문 바로가기

Enginius/Python&TensorFlow

Simple Handshaking between Matlab and TensorFlow

Simple handshaking scripts between Matlab and Python (Tensorflow). It is assumed that two processes (or machines) are virtually connected and synced via some external programs, such as Dropbox. A simple finite state machine is used to come up with this handshaking process. 

 손에 손 잡고~ 


1. MATLAB Side Code

 

% CONFIGURATION

step_matlab = 1; % FOR HANDSHAKING

savename = 'tensorflow_code/data/mats/step_fr_matlab.mat';

loadname = 'tensorflow_code/data/mats/step_fr_tf.mat';

 

% RUN FINITE STATE MACHINE

FSM_STEP = 0; FSM_STEP_PREV = 0;

while 1

    switch FSM_STEP

        case 0 % INITIALIZE 

            msg  = 'THIS IS FROM MATLAB';

            save(savename, 'step_matlab', 'msg');

            fprintf('[0] %s SAVED. STEP IS [%d]. \n' ...

                , savename, step_matlab);

            FSM_STEP = 1;

        case 1 % LOAD FROM TENSORFLOW (CHECK UPDATE)

            if exist(loadname, 'file')

                l = load(loadname);

                step_tf = l.step_tf;

                if step_tf > step_matlab

                    % IF TF GAVE BIGGER STEP, 

                    FSM_STEP = 2;

                end

            else

                % fprintf('%s DOES NOT EXIST. \n', loadname);

            end

        case 2 % DO SOME ACTION HERE 

            % HERE, WE JUST INCREASE THE STEP

            step_matlab = step_tf + 1; 

            msg  = 'THIS IS FROM MATLAB';

            FSM_STEP = 3;

            fprintf(2, '[2] STEP_MATLAB IS [%d] \n', step_matlab);

        case 3 % SAVE TO TENSORFLOW 

            save(savename, 'step_matlab', 'msg');

            FSM_STEP = 1;

    end

    if FSM_STEP ~= FSM_STEP_PREV % PRINT STEP TRANSITIONS

        fprintf('FSM STEP CHANGE FROM [%d] TO [%d] \n' ...

            , FSM_STEP_PREV, FSM_STEP);

        FSM_STEP_PREV = FSM_STEP;

    end

    pause(5);

end


2. Python Side Code

 

import os

import time

import scipy.io 

print ("PACKAGES LOADED")


# CONFIGURATION

loadname = './data/mats/step_fr_matlab.mat'

savename = './data/mats/step_fr_tf.mat'

step_tf  = 0


# RUN FINITE STATE MACHINE

FSM_STEP = 0

FSM_STEP_PREV= FSM_STEP

while True:

    if FSM_STEP is 0: # INITIALIZATION

        FSM_STEP = 1

    elif FSM_STEP is 1: # CHECK MATLAB UPDATE

        # LOAD 

        if os.path.exists(loadname) is False:

            print ("NO FILE EXISTS!")

        else:

            l = scipy.io.loadmat(loadname)

            step_matlab = l['step_matlab'][0]

        # CHECK

        if step_matlab > step_tf:

            FSM_STEP = 2;

    elif FSM_STEP is 2: # DO SOME ACTIONS HERE

        step_tf = step_matlab + 1

        FSM_STEP = 3

        print ("STEP_TF IS [%d]" % (step_tf))

    elif FSM_STEP is 3: # SAVE

        scipy.io.savemat(savename, mdict={'step_tf': step_tf})

        FSM_STEP = 1

    else:

        print ("FSM_STEP SHOULD NOT BE [%d]." % (FSM_STEP))

    

    # PRINT TRANSITIONS

    if FSM_STEP_PREV != FSM_STEP:

        print ("FSM_STEP [%d] => [%d]" % (FSM_STEP_PREV, FSM_STEP))

        FSM_STEP_PREV = FSM_STEP

    # TIME DELAY (ONE SECOND)

    time.sleep(5)




'Enginius > Python&TensorFlow' 카테고리의 다른 글

Keyboard Event  (0) 2017.09.07
SUPER GPU MACHINE 만들기  (0) 2017.08.11
TensorFlow-trained network in MATLAB  (0) 2017.01.10
도커와 텐서플로우 설치하기  (0) 2016.11.30
GPU 머신 조립하기  (2) 2016.11.25