{"action":"created","issue":{"url":"https://api.github.com/repos/gkioxari/RstarCNN/issues/12","repository_url":"https://api.github.com/repos/gkioxari/RstarCNN","labels_url":"https://api.github.com/repos/gkioxari/RstarCNN/issues/12/labels{/name}","comments_url":"https://api.github.com/repos/gkioxari/RstarCNN/issues/12/comments","events_url":"https://api.github.com/repos/gkioxari/RstarCNN/issues/12/events","html_url":"https://github.com/gkioxari/RstarCNN/issues/12","id":156506706,"number":12,"title":"How can I plot the loss?","user":{"login":"Christmas-Carol","id":3291902,"avatar_url":"https://avatars.githubusercontent.com/u/3291902?v=3","gravatar_id":"","url":"https://api.github.com/users/Christmas-Carol","html_url":"https://github.com/Christmas-Carol","followers_url":"https://api.github.com/users/Christmas-Carol/followers","following_url":"https://api.github.com/users/Christmas-Carol/following{/other_user}","gists_url":"https://api.github.com/users/Christmas-Carol/gists{/gist_id}","starred_url":"https://api.github.com/users/Christmas-Carol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Christmas-Carol/subscriptions","organizations_url":"https://api.github.com/users/Christmas-Carol/orgs","repos_url":"https://api.github.com/users/Christmas-Carol/repos","events_url":"https://api.github.com/users/Christmas-Carol/events{/privacy}","received_events_url":"https://api.github.com/users/Christmas-Carol/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-05-24T13:26:34Z","updated_at":"2016-06-12T03:02:32Z","closed_at":null,"body":"Hi, thanks for your wonderful work.\r\n\r\nI've reproduced your work and I found that you have changed the snapshot writing part. \r\nThen how can I plot the two loss?\r\n\r\nFrom my deep heart, I know I just need to change the default tools/extra/plot_training_log.py.example file.\r\n\r\nCan you share your script?\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/gkioxari/RstarCNN/issues/comments/225407008","html_url":"https://github.com/gkioxari/RstarCNN/issues/12#issuecomment-225407008","issue_url":"https://api.github.com/repos/gkioxari/RstarCNN/issues/12","id":225407008,"user":{"login":"Christmas-Carol","id":3291902,"avatar_url":"https://avatars.githubusercontent.com/u/3291902?v=3","gravatar_id":"","url":"https://api.github.com/users/Christmas-Carol","html_url":"https://github.com/Christmas-Carol","followers_url":"https://api.github.com/users/Christmas-Carol/followers","following_url":"https://api.github.com/users/Christmas-Carol/following{/other_user}","gists_url":"https://api.github.com/users/Christmas-Carol/gists{/gist_id}","starred_url":"https://api.github.com/users/Christmas-Carol/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Christmas-Carol/subscriptions","organizations_url":"https://api.github.com/users/Christmas-Carol/orgs","repos_url":"https://api.github.com/users/Christmas-Carol/repos","events_url":"https://api.github.com/users/Christmas-Carol/events{/privacy}","received_events_url":"https://api.github.com/users/Christmas-Carol/received_events","type":"User","site_admin":false},"created_at":"2016-06-12T03:02:32Z","updated_at":"2016-06-12T03:02:32Z","body":"my version of plot script:\r\n`#!/usr/bin/env python\r\nimport inspect\r\nimport os\r\nimport random\r\nimport sys\r\nimport matplotlib.cm as cmx\r\nimport matplotlib.colors as colors\r\nimport matplotlib.pyplot as plt\r\nimport matplotlib.legend as lgd\r\nimport matplotlib.markers as mks\r\n\r\ndef get_log_parsing_script():\r\n    dirname = os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))\r\n    return dirname + '/parse_log.sh'\r\n\r\ndef get_log_file_suffix():\r\n    return '.log'\r\n\r\ndef get_chart_type_description_separator():\r\n    return '  vs. '\r\n\r\ndef is_x_axis_field(field):\r\n    x_axis_fields = ['Iters', 'Seconds']\r\n    return field in x_axis_fields\r\n\r\ndef create_field_index():\r\n    train_key = 'Train'\r\n    test_key = 'Test'\r\n    field_index = {train_key:{'Iters':0, 'Seconds':1, train_key + ' loss_cls':2,\r\n                              train_key + ' loss_bbox':2,\r\n                              train_key + ' learning rate':3},\r\n                   test_key:{'Iters':0, 'Seconds':1, test_key + ' accuracy':2,\r\n                             test_key + ' loss_cls':2, test_key + ' loss_bbox':2}}\r\n    fields = set()\r\n    for data_file_type in field_index.keys():\r\n        fields = fields.union(set(field_index[data_file_type].keys()))\r\n    fields = list(fields)\r\n    fields.sort()\r\n    return field_index, fields\r\n#\r\n# def create_field_index():\r\n#     train_key = 'Train'\r\n#     test_key = 'Test'\r\n#     field_index = {train_key:{'Iters':0, 'Seconds':1, train_key + 'loss':2,\r\n#                               }}\r\n\r\ndef get_supported_chart_types():\r\n    field_index, fields = create_field_index()\r\n    num_fields = len(fields)\r\n    supported_chart_types = []\r\n    for i in xrange(num_fields):\r\n        if not is_x_axis_field(fields[i]):\r\n            for j in xrange(num_fields):\r\n                if i != j and is_x_axis_field(fields[j]):\r\n                    supported_chart_types.append('%s%s%s' % (\r\n                        fields[i], get_chart_type_description_separator(),\r\n                        fields[j]))\r\n    return supported_chart_types\r\n\r\ndef get_chart_type_description(chart_type):\r\n    supported_chart_types = get_supported_chart_types()\r\n    chart_type_description = supported_chart_types[chart_type]\r\n    return chart_type_description\r\n\r\ndef get_data_file_type(chart_type):\r\n    description = get_chart_type_description(chart_type)\r\n    data_file_type = description.split()[0]\r\n    return data_file_type\r\n\r\ndef get_data_file(chart_type, path_to_log):\r\n    return os.path.basename(path_to_log) + '.' + get_data_file_type(chart_type).lower()\r\n\r\ndef get_field_descriptions(chart_type):\r\n    description = get_chart_type_description(chart_type).split(\r\n        get_chart_type_description_separator())\r\n    y_axis_field = description[0]\r\n    x_axis_field = description[1]\r\n    return x_axis_field, y_axis_field    \r\n\r\ndef get_field_indecies(x_axis_field, y_axis_field):    \r\n    data_file_type = get_data_file_type(chart_type)\r\n    fields = create_field_index()[0][data_file_type]\r\n    return fields[x_axis_field], fields[y_axis_field]\r\n\r\ndef load_data(data_file, field_idx0, field_idx1):\r\n    data = [[], []]\r\n    with open(data_file, 'r') as f:\r\n        for line in f:\r\n            line = line.strip()\r\n            if line[0] != '#':\r\n                fields = line.split()\r\n                data[0].append(float(fields[field_idx0].strip()))\r\n                data[1].append(float(fields[field_idx1].strip()))\r\n    return data\r\n\r\ndef random_marker():\r\n    markers = mks.MarkerStyle.markers\r\n    num = len(markers.values())\r\n    idx = random.randint(0, num - 1)\r\n    return markers.values()[idx]\r\n\r\ndef get_data_label(path_to_log):\r\n    label = path_to_log[path_to_log.rfind('/')+1 : path_to_log.rfind(\r\n        get_log_file_suffix())]\r\n    return label\r\n\r\ndef get_legend_loc(chart_type):\r\n    x_axis, y_axis = get_field_descriptions(chart_type)\r\n    loc = 'lower right'\r\n    if y_axis.find('accuracy') != -1:\r\n        pass\r\n    if y_axis.find('loss') != -1 or y_axis.find('learning rate') != -1:\r\n        loc = 'upper right'\r\n    return loc\r\n\r\ndef plot_chart(chart_type, path_to_png, path_to_log_list):\r\n    for path_to_log in path_to_log_list:\r\n        os.system('%s %s' % (get_log_parsing_script(), path_to_log))\r\n        data_file = get_data_file(chart_type, path_to_log)\r\n        x_axis_field, y_axis_field = get_field_descriptions(chart_type)\r\n        x, y = get_field_indecies(x_axis_field, y_axis_field)\r\n        data = load_data(data_file, x, y)\r\n        ## TODO: more systematic color cycle for lines\r\n        color = [random.random(), random.random(), random.random()]\r\n        label = get_data_label(path_to_log)\r\n        linewidth = 0.75\r\n        ## If there too many datapoints, do not use marker.\r\n##        use_marker = False\r\n        use_marker = True\r\n        if not use_marker:\r\n            plt.plot(data[0], data[1], label = label, color = color,\r\n                     linewidth = linewidth)\r\n        else:\r\n            ok = False\r\n            ## Some markers throw ValueError: Unrecognized marker style\r\n            while not ok:\r\n                try:\r\n                    marker = random_marker()\r\n                    plt.plot(data[0], data[1], label = label, color = color,\r\n                             marker = marker, linewidth = linewidth)\r\n                    ok = True\r\n                except:\r\n                    pass\r\n    legend_loc = get_legend_loc(chart_type)\r\n    plt.legend(loc = legend_loc, ncol = 1) # ajust ncol to fit the space\r\n    plt.title(get_chart_type_description(chart_type))\r\n    plt.xlabel(x_axis_field)\r\n    plt.ylabel(y_axis_field)  \r\n    plt.savefig(path_to_png)     \r\n    plt.show()\r\n\r\ndef print_help():\r\n    print \"\"\"This script mainly serves as the basis of your customizations.\r\nCustomization is a must.\r\nYou can copy, paste, edit them in whatever way you want.\r\nBe warned that the fields in the training log may change in the future.\r\nYou had better check the data files and change the mapping from field name to\r\n field index in create_field_index before designing your own plots.\r\nUsage:\r\n    ./plot_log.sh chart_type[0-%s] /where/to/save.png /path/to/first.log ...\r\nNotes:\r\n    1. Supporting multiple logs.\r\n    2. Log file name must end with the lower-cased \"%s\".\r\nSupported chart types:\"\"\" % (len(get_supported_chart_types()) - 1,\r\n                             get_log_file_suffix())\r\n    supported_chart_types = get_supported_chart_types()\r\n    num = len(supported_chart_types)\r\n    for i in xrange(num):\r\n        print '    %d: %s' % (i, supported_chart_types[i])\r\n    exit\r\n\r\ndef is_valid_chart_type(chart_type):\r\n    return chart_type >= 0 and chart_type < len(get_supported_chart_types())\r\n  \r\nif __name__ == '__main__':\r\n    if len(sys.argv) < 4:\r\n        print_help()\r\n    else:\r\n        chart_type = int(sys.argv[1])\r\n        if not is_valid_chart_type(chart_type):\r\n            print_help()\r\n        path_to_png = sys.argv[2]\r\n        if not path_to_png.endswith('.png'):\r\n            print 'Path must ends with png' % path_to_png\r\n            exit            \r\n        path_to_logs = sys.argv[3:]\r\n        for path_to_log in path_to_logs:\r\n            if not os.path.exists(path_to_log):\r\n                print 'Path does not exist: %s' % path_to_log\r\n                exit\r\n            if not path_to_log.endswith(get_log_file_suffix()):\r\n                print_help()\r\n        ## plot_chart accpets multiple path_to_logs\r\n        plot_chart(chart_type, path_to_png, path_to_logs)\r\n`"}}
{"actor":{"display_login":"Christmas-Carol"}}