<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2016 The AOKP Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at

          http://www.apache.org/licenses/LICENSE-2.0

     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<PreferenceScreen
    android:title="@string/ui_settings"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <ListPreference
        android:key="listview_animation"
        android:dialogTitle="@string/listview_animation_title"
        android:title="@string/listview_animation_title"
        android:entries="@array/listview_animation_entries"
        android:entryValues="@array/listview_animation_values"
        android:defaultValue="0" />

    <ListPreference
        android:key="listview_interpolator"
        android:dialogTitle="@string/listview_interpolator_title"
        android:title="@string/listview_interpolator_title"
        android:entries="@array/listview_interpolator_entries"
        android:entryValues="@array/listview_interpolator_values"
        android:defaultValue="0" />

    <ListPreference
        android:key="pref_scrollingcache"
        android:dialogTitle="@string/pref_scrollingcache_title"
        android:title="@string/pref_scrollingcache_title"
        android:summary="@string/pref_scrollingcache_summary"
        android:entries="@array/entries_scrollingcache"
        android:entryValues="@array/values_scrollingcache" />

    <SwitchPreference
        android:key="disable_transition_animations"
        android:title="@string/disable_animations"
        android:summary="@string/disable_animations_summary"
        android:defaultValue="false" />

    <ListPreference
        android:key="screen_off_animation"
        android:dialogTitle="@string/screen_off_animation_title"
        android:title="@string/screen_off_animation_title"
        android:summary="%s"
        android:entries="@array/screen_off_animation_entries"
        android:entryValues="@array/screen_off_animation_values" />

    <com.aokp.romcontrol.settings.SystemSettingSwitchPreference
        android:key="force_expanded_notifications"
        android:title="@string/force_expanded_notifications_title"
        android:summary="@string/force_expanded_notifications_summary"
        android:defaultValue="false" />

    <com.aokp.romcontrol.settings.SystemSettingSwitchPreference
        android:key="disable_immersive_message"
        android:title="@string/disable_immersive_message_title"
        android:summary="@string/disable_immersive_message_summary"
        android:defaultValue="false" />

    <PreferenceCategory
        android:title="@string/toast_settings_title">
        <ListPreference
            android:key="toast_animation"
            android:title="@string/toast_animation_title"
            android:entries="@array/toast_animation_entries"
            android:entryValues="@array/toast_animation_values"
            android:persistent="false" />

        <com.aokp.romcontrol.settings.SystemSettingSwitchPreference
            android:key="toast_icon"
            android:title="@string/toast_icon_title"
            android:summary="@string/toast_icon_summary"
            android:defaultValue="true" />

        <net.margaritov.preference.colorpicker.ColorPickerPreference
            android:key="toast_icon_color"
            android:title="@string/toast_icon_color"
            android:defaultValue="0xffffffff"
            android:dependency="toast_icon" />

        <net.margaritov.preference.colorpicker.ColorPickerPreference
            android:key="toast_text_color"
            android:title="@string/toast_text_color"
            android:defaultValue="0xffffffff" />
    </PreferenceCategory>

</PreferenceScreen>

{'repo_name': 'AOKP/packages_apps_ROMControl', 'stars': '104', 'repo_language': 'Java', 'file_name': 'gradle-wrapper.properties', 'mime_type': 'text/plain', 'hash': -857124659805251367, 'source_dataset': 'data'}


@deftypefun {time_t} {gnutls_certificate_get_ocsp_expiration} (gnutls_certificate_credentials_t @var{sc}, unsigned @var{idx}, int @var{oidx}, unsigned @var{flags})
@var{sc}: is a credentials structure.

@var{idx}: is a certificate chain index as returned by @code{gnutls_certificate_set_key()}  and friends

@var{oidx}: is an OCSP response index

@var{flags}: should be zero

This function returns the validity of the loaded OCSP responses,
to provide information on when to reload/refresh them.

Note that the credentials structure should be read-only when in
use, thus when reloading, either the credentials structure must not
be in use by any sessions, or a new credentials structure should be
allocated for new sessions.

When  @code{oidx} is (-1) then the minimum refresh time for all responses
is returned. Otherwise the index specifies the response corresponding
to the  @code{odix} certificate in the certificate chain.

@strong{Returns:} On success, the expiration time of the OCSP response. Otherwise
(time_t)(-1) on error, or (time_t)-2 on out of bounds.

@strong{Since:} 3.6.3
@end deftypefun

{'repo_name': 'tanersener/mobile-ffmpeg', 'stars': '1841', 'repo_language': 'C', 'file_name': 'SDL_naclvideo.h', 'mime_type': 'text/x-c', 'hash': 919760134253020317, 'source_dataset': 'data'}


package org.frameworkset.elasticsearch.handler;
/**
 * Copyright 2008 biaoping.yin
 *

 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *

 * http://www.apache.org/licenses/LICENSE-2.0
 *

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.util.EntityUtils;
import org.frameworkset.spi.remote.http.URLResponseHandler;

import java.io.IOException;

/**
 *
Description:

 *

 *
Copyright (c) 2018

 * @Date 2018/7/19 15:20
 * @author biaoping.yin
 * @version 1.0
 */
public class ESStringResponseHandler extends BaseExceptionResponseHandler implements URLResponseHandler<String>,ESExceptionWrapper {

	public ESStringResponseHandler() {
		// TODO Auto-generated constructor stub
	}
	private String charSet;
	public ESStringResponseHandler(String charSet) {
		// TODO Auto-generated constructor stub
		this.charSet = charSet;
	}

	@Override
	public String handleResponse(final HttpResponse response)
			throws ClientProtocolException, IOException {
		int status = initStatus(  response);

		if (status >= 200 && status < 300) {
			HttpEntity entity = response.getEntity();

			if( entity != null) {
				if(charSet == null)
					return EntityUtils.toString(entity);
				else
					return EntityUtils.toString(entity,charSet);
			}
			else {
				return null;
			}
		} else {
			HttpEntity entity = response.getEntity();
//			if(status == 404){
//				this.elasticSearchException = new ElasticSearchException(EntityUtils.toString(entity),status);
//				return null;
//			}
//			else {
//				if (entity != null)
//					throw new ElasticSearchException(EntityUtils.toString(entity), status);
//				else
//					throw new ElasticSearchException("Unexpected response status: " + status, status);
//			}
			return (String)handleException(url,entity ,status,charSet);
		}
	}

}

{'repo_name': 'bbossgroups/bboss-elasticsearch', 'stars': '323', 'repo_language': 'Java', 'file_name': 'application.properties', 'mime_type': 'text/plain', 'hash': -8560217977911428083, 'source_dataset': 'data'}


package matchers

import (
	"fmt"

	"github.com/onsi/gomega/format"
	"github.com/onsi/gomega/internal/oraclematcher"
	"github.com/onsi/gomega/types"
)

type OrMatcher struct {
	Matchers []types.GomegaMatcher

	// state
	firstSuccessfulMatcher types.GomegaMatcher
}

func (m *OrMatcher) Match(actual interface{}) (success bool, err error) {
	m.firstSuccessfulMatcher = nil
	for _, matcher := range m.Matchers {
		success, err := matcher.Match(actual)
		if err != nil {
			return false, err
		}
		if success {
			m.firstSuccessfulMatcher = matcher
			return true, nil
		}
	}
	return false, nil
}

func (m *OrMatcher) FailureMessage(actual interface{}) (message string) {
	// not the most beautiful list of matchers, but not bad either...
	return format.Message(actual, fmt.Sprintf("To satisfy at least one of these matchers: %s", m.Matchers))
}

func (m *OrMatcher) NegatedFailureMessage(actual interface{}) (message string) {
	return m.firstSuccessfulMatcher.NegatedFailureMessage(actual)
}

func (m *OrMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
	/*
		Example with 3 matchers: A, B, C

		Match evaluates them: F, T, <?>  => T
		So match is currently T, what should MatchMayChangeInTheFuture() return?
		Seems like it only depends on B, since currently B MUST change to allow the result to become F

		Match eval: F, F, F  => F
		So match is currently F, what should MatchMayChangeInTheFuture() return?
		Seems to depend on ANY of them being able to change to T.
	*/

	if m.firstSuccessfulMatcher != nil {
		// one of the matchers succeeded.. it must be able to change in order to affect the result
		return oraclematcher.MatchMayChangeInTheFuture(m.firstSuccessfulMatcher, actual)
	} else {
		// so all matchers failed.. Any one of them changing would change the result.
		for _, matcher := range m.Matchers {
			if oraclematcher.MatchMayChangeInTheFuture(matcher, actual) {
				return true
			}
		}
		return false // none of were going to change
	}
}

{'repo_name': 'kubernetes-sigs/cri-tools', 'stars': '432', 'repo_language': 'Go', 'file_name': 'main_windows.go', 'mime_type': 'text/plain', 'hash': -5757947735901192354, 'source_dataset': 'data'}


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE pkgmetadata SYSTEM "http://www.gentoo.org/dtd/metadata.dtd">
<pkgmetadata>
  <maintainer type="project">
    <email>cjk@gentoo.org</email>
    <name>Cjk</name>
  </maintainer>
  <upstream>
    <remote-id type="github">sunpinyin/sunpinyin</remote-id>
  </upstream>
</pkgmetadata>

{'repo_name': 'gentoo/gentoo', 'stars': '1217', 'repo_language': 'Shell', 'file_name': 'perl-Attribute-Handlers-1.10.0-r3.ebuild', 'mime_type': 'text/plain', 'hash': -7047918476279320527, 'source_dataset': 'data'}


import pytest

from openshift_checks import OpenShiftCheck, OpenShiftCheckException
from openshift_checks.mixins import NotContainerizedMixin

class NotContainerizedCheck(NotContainerizedMixin, OpenShiftCheck):
    name = "not_containerized"
    run = NotImplemented

@pytest.mark.parametrize('task_vars,expected', [
    (dict(openshift_is_atomic=False), True),
    (dict(openshift_is_atomic=True), False),
])
def test_is_active(task_vars, expected):
    assert NotContainerizedCheck(None, task_vars).is_active() == expected

def test_is_active_missing_task_vars():
    with pytest.raises(OpenShiftCheckException) as excinfo:
        NotContainerizedCheck().is_active()
    assert 'openshift_is_atomic' in str(excinfo.value)

{'repo_name': 'openshift/openshift-tools', 'stars': '146', 'repo_language': 'Python', 'file_name': 'README.md', 'mime_type': 'text/plain', 'hash': 4353983226047588922, 'source_dataset': 'data'}


{% extends 'django/base.html' %}

{% block content %}

Pontoon

Sign in

{% if form.non_field_errors %}

        {% for error in form.non_field_errors.values %}

{{ error }}

        {% endfor %}

{% endif %}

<form id="standalone-sign-in" method="POST" action="{{ request.path }}">

        Username

        {{ form.username }}
        {{ form.username.errors }}

        Password

        {{ form.password }}
        {{ form.password.errors }}

        <input class="button active" type="submit" name="sign-in" id="sign-in" value="Sign in" />

    <input type="hidden" name="next" value="{{ request.GET.next }}">
    {% csrf_token %}
</form>
{% endblock content %}

{'repo_name': 'mozilla/pontoon', 'stars': '842', 'repo_language': 'Python', 'file_name': 'dev.txt', 'mime_type': 'text/plain', 'hash': 4589935332096785554, 'source_dataset': 'data'}


/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.drill.exec.work.batch;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import org.apache.drill.common.AutoCloseables;
import org.apache.drill.common.concurrent.AutoCloseableLock;
import org.apache.drill.exec.exception.FragmentSetupException;
import org.apache.drill.exec.memory.BufferAllocator;
import org.apache.drill.exec.ops.FragmentContext;
import org.apache.drill.exec.proto.BitControl.Collector;
import org.apache.drill.exec.proto.BitControl.PlanFragment;
import org.apache.drill.exec.record.RawFragmentBatch;
import org.apache.drill.exec.rpc.data.IncomingDataBatch;

import org.apache.drill.shaded.guava.com.google.common.collect.ImmutableMap;
import org.apache.drill.shaded.guava.com.google.common.collect.Maps;

/**
 * Determines when a particular fragment has enough data for each of its receiving exchanges to commence execution.  Also monitors whether we've collected all incoming data.
 */
public class IncomingBuffers implements AutoCloseable {
  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(IncomingBuffers.class);

  private volatile boolean closed = false;
  private final AtomicInteger streamsRemaining = new AtomicInteger(0);
  private final AtomicInteger remainingRequired;
  private final Map<Integer, DataCollector> collectorMap;
  private final FragmentContext context;

  /**
   * Lock used to manage close and data acceptance. We should only create a local reference to incoming data in the case
   * that the incoming buffers are !closed. As such, we need to make sure that we aren't in the process of closing the
   * incoming buffers when data is arriving. The read lock can be shared by many incoming batches but the write lock
   * must be exclusive to the close method.
   */
  private final ReadWriteLock lock = new ReentrantReadWriteLock();
  private final AutoCloseableLock sharedIncomingBatchLock = new AutoCloseableLock(lock.readLock());
  private final AutoCloseableLock exclusiveCloseLock = new AutoCloseableLock(lock.writeLock());

  public IncomingBuffers(PlanFragment fragment, FragmentContext context) {
    this.context = context;
    Map<Integer, DataCollector> collectors = Maps.newHashMap();
    remainingRequired = new AtomicInteger(fragment.getCollectorCount());
    for(int i =0; i < fragment.getCollectorCount(); i++){
      Collector collector = fragment.getCollector(i);
      DataCollector newCollector = collector.getSupportsOutOfOrder() ?
          new MergingCollector(remainingRequired, collector, context) :
          new PartitionedCollector(remainingRequired, collector, context);
      collectors.put(collector.getOppositeMajorFragmentId(), newCollector);
    }

    logger.debug("Came up with a list of {} required fragments.  Fragments {}", remainingRequired.get(), collectors);
    collectorMap = ImmutableMap.copyOf(collectors);

    // Determine the total number of incoming streams that will need to be completed before we are finished.
    int totalStreams = 0;
    for (DataCollector bc : collectorMap.values()) {
      totalStreams += bc.getTotalIncomingFragments();
    }
    assert totalStreams >= remainingRequired.get() : String.format("Total Streams %d should be more than the minimum number of streams to commence (%d).  It isn't.", totalStreams, remainingRequired.get());
    streamsRemaining.set(totalStreams);
  }

  public boolean batchArrived(final IncomingDataBatch incomingBatch) throws FragmentSetupException, IOException {

    // we want to make sure that we only generate local record batch reference in the case that we're not closed.
    // Otherwise we would leak memory.
    try (@SuppressWarnings("unused") AutoCloseables.Closeable lock = sharedIncomingBatchLock.open()) {
      if (closed) {
        return false;
      }

      if (incomingBatch.getHeader().getIsLastBatch()) {
        streamsRemaining.decrementAndGet();
      }

      final int sendMajorFragmentId = incomingBatch.getHeader().getSendingMajorFragmentId();
      DataCollector collector = collectorMap.get(sendMajorFragmentId);
      if (collector == null) {
        throw new FragmentSetupException(String.format(
            "We received a major fragment id that we were not expecting.  The id was %d. %s", sendMajorFragmentId,
            Arrays.toString(collectorMap.values().toArray())));
      }

      // Use the Data Collector's buffer allocator if set, otherwise the fragment's one
      BufferAllocator ownerAllocator = collector.getAllocator();

      synchronized (collector) {
        final RawFragmentBatch newRawFragmentBatch = incomingBatch.newRawFragmentBatch(ownerAllocator);
        boolean decrementedToZero = collector
            .batchArrived(incomingBatch.getHeader().getSendingMinorFragmentId(), newRawFragmentBatch);
        newRawFragmentBatch.release();

        // we should only return true if remaining required has been decremented and is currently equal to zero.
        return decrementedToZero;
      }

    }

  }

  public int getRemainingRequired() {
    int rem = remainingRequired.get();
    if (rem < 0) {
      return 0;
    }
    return rem;
  }

  public DataCollector getCollector(int senderMajorFragmentId) {
    return collectorMap.get(senderMajorFragmentId);
  }

  public boolean isDone() {
    return streamsRemaining.get() < 1;
  }

  @Override
  public void close() throws Exception {
    try (@SuppressWarnings("unused") AutoCloseables.Closeable lock = exclusiveCloseLock.open()) {
      closed = true;
      AutoCloseables.close(collectorMap.values());
    }
  }

}

{'repo_name': 'apache/drill', 'stars': '1417', 'repo_language': 'Java', 'file_name': 'TestConverterUtil.java', 'mime_type': 'text/x-java', 'hash': -492272940831858633, 'source_dataset': 'data'}


fileFormatVersion: 2
guid: e14581d7fdab3b948b115039898883dc
MonoImporter:
  externalObjects: {}
  serializedVersion: 2
  defaultReferences: []
  executionOrder: 0
  icon: {instanceID: 0}
  userData:
  assetBundleName:
  assetBundleVariant:

{'repo_name': 'jarjin/FinalFramework', 'stars': '359', 'repo_language': 'C#', 'file_name': 'addressbook.proto', 'mime_type': 'text/plain', 'hash': -8472213758616376679, 'source_dataset': 'data'}


// Copyright (c) 2007, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//  MachIPC.mm
//  Wrapper for mach IPC calls

#import <stdio.h>
#import "MachIPC.h"
#include "common/mac/bootstrap_compat.h"

namespace google_breakpad {
//==============================================================================
MachSendMessage::MachSendMessage(int32_t message_id) : MachMessage() {
  head.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);

  // head.msgh_remote_port = ...; // filled out in MachPortSender::SendMessage()
  head.msgh_local_port = MACH_PORT_NULL;
  head.msgh_reserved = 0;
  head.msgh_id = 0;

  SetDescriptorCount(0);  // start out with no descriptors

  SetMessageID(message_id);
  SetData(NULL, 0);       // client may add data later
}

//==============================================================================
// returns true if successful
bool MachMessage::SetData(void *data,
                          int32_t data_length) {
  // first check to make sure we have enough space
  size_t size = CalculateSize();
  size_t new_size = size + data_length;

  if (new_size > sizeof(MachMessage)) {
    return false;  // not enough space
  }

  GetDataPacket()->data_length = EndianU32_NtoL(data_length);
  if (data) memcpy(GetDataPacket()->data, data, data_length);

  CalculateSize();

  return true;
}

//==============================================================================
// calculates and returns the total size of the message
// Currently, the entire message MUST fit inside of the MachMessage
//    messsage size <= sizeof(MachMessage)
mach_msg_size_t MachMessage::CalculateSize() {
  size_t size = sizeof(mach_msg_header_t) + sizeof(mach_msg_body_t);

  // add space for MessageDataPacket
  int32_t alignedDataLength = (GetDataLength() + 3) & ~0x3;
  size += 2*sizeof(int32_t) + alignedDataLength;

  // add space for descriptors
  size += GetDescriptorCount() * sizeof(MachMsgPortDescriptor);

  head.msgh_size = static_cast<mach_msg_size_t>(size);

  return head.msgh_size;
}

//==============================================================================
MachMessage::MessageDataPacket *MachMessage::GetDataPacket() {
  size_t desc_size = sizeof(MachMsgPortDescriptor)*GetDescriptorCount();
  MessageDataPacket *packet =
    reinterpret_cast<MessageDataPacket*>(padding + desc_size);

  return packet;
}

//==============================================================================
void MachMessage::SetDescriptor(int n,
                                const MachMsgPortDescriptor &desc) {
  MachMsgPortDescriptor *desc_array =
    reinterpret_cast<MachMsgPortDescriptor*>(padding);
  desc_array[n] = desc;
}

//==============================================================================
// returns true if successful otherwise there was not enough space
bool MachMessage::AddDescriptor(const MachMsgPortDescriptor &desc) {
  // first check to make sure we have enough space
  int size = CalculateSize();
  size_t new_size = size + sizeof(MachMsgPortDescriptor);

  if (new_size > sizeof(MachMessage)) {
    return false;  // not enough space
  }

  // unfortunately, we need to move the data to allow space for the
  // new descriptor
  u_int8_t *p = reinterpret_cast<u_int8_t*>(GetDataPacket());
  bcopy(p, p+sizeof(MachMsgPortDescriptor), GetDataLength()+2*sizeof(int32_t));

  SetDescriptor(GetDescriptorCount(), desc);
  SetDescriptorCount(GetDescriptorCount() + 1);

  CalculateSize();

  return true;
}

//==============================================================================
void MachMessage::SetDescriptorCount(int n) {
  body.msgh_descriptor_count = n;

  if (n > 0) {
    head.msgh_bits |= MACH_MSGH_BITS_COMPLEX;
  } else {
    head.msgh_bits &= ~MACH_MSGH_BITS_COMPLEX;
  }
}

//==============================================================================
MachMsgPortDescriptor *MachMessage::GetDescriptor(int n) {
  if (n < GetDescriptorCount()) {
    MachMsgPortDescriptor *desc =
      reinterpret_cast<MachMsgPortDescriptor*>(padding);
    return desc + n;
  }

  return nil;
}

//==============================================================================
mach_port_t MachMessage::GetTranslatedPort(int n) {
  if (n < GetDescriptorCount()) {
    return GetDescriptor(n)->GetMachPort();
  }
  return MACH_PORT_NULL;
}

#pragma mark -

//==============================================================================
// create a new mach port for receiving messages and register a name for it
ReceivePort::ReceivePort(const char *receive_port_name) {
  mach_port_t current_task = mach_task_self();

  init_result_ = mach_port_allocate(current_task,
                                    MACH_PORT_RIGHT_RECEIVE,
                                    &port_);

  if (init_result_ != KERN_SUCCESS)
    return;

  init_result_ = mach_port_insert_right(current_task,
                                        port_,
                                        port_,
                                        MACH_MSG_TYPE_MAKE_SEND);

  if (init_result_ != KERN_SUCCESS)
    return;

  mach_port_t task_bootstrap_port = 0;
  init_result_ = task_get_bootstrap_port(current_task, &task_bootstrap_port);

  if (init_result_ != KERN_SUCCESS)
    return;

  init_result_ = breakpad::BootstrapRegister(
      bootstrap_port,
      const_cast<char*>(receive_port_name),
      port_);
}

//==============================================================================
// create a new mach port for receiving messages
ReceivePort::ReceivePort() {
  mach_port_t current_task = mach_task_self();

  init_result_ = mach_port_allocate(current_task,
                                    MACH_PORT_RIGHT_RECEIVE,
                                    &port_);

  if (init_result_ != KERN_SUCCESS)
    return;

  init_result_ =   mach_port_insert_right(current_task,
                                          port_,
                                          port_,
                                          MACH_MSG_TYPE_MAKE_SEND);
}

//==============================================================================
// Given an already existing mach port, use it.  We take ownership of the
// port and deallocate it in our destructor.
ReceivePort::ReceivePort(mach_port_t receive_port)
  : port_(receive_port),
    init_result_(KERN_SUCCESS) {
}

//==============================================================================
ReceivePort::~ReceivePort() {
  if (init_result_ == KERN_SUCCESS)
    mach_port_deallocate(mach_task_self(), port_);
}

//==============================================================================
kern_return_t ReceivePort::WaitForMessage(MachReceiveMessage *out_message,
                                          mach_msg_timeout_t timeout) {
  if (!out_message) {
    return KERN_INVALID_ARGUMENT;
  }

  // return any error condition encountered in constructor
  if (init_result_ != KERN_SUCCESS)
    return init_result_;

  out_message->head.msgh_bits = 0;
  out_message->head.msgh_local_port = port_;
  out_message->head.msgh_remote_port = MACH_PORT_NULL;
  out_message->head.msgh_reserved = 0;
  out_message->head.msgh_id = 0;

  mach_msg_option_t options = MACH_RCV_MSG;
  if (timeout != MACH_MSG_TIMEOUT_NONE)
    options |= MACH_RCV_TIMEOUT;
  kern_return_t result = mach_msg(&out_message->head,
                                  options,
                                  0,
                                  sizeof(MachMessage),
                                  port_,
                                  timeout,              // timeout in ms
                                  MACH_PORT_NULL);

  return result;
}

#pragma mark -

//==============================================================================
// get a port with send rights corresponding to a named registered service
MachPortSender::MachPortSender(const char *receive_port_name) {
  mach_port_t task_bootstrap_port = 0;
  init_result_ = task_get_bootstrap_port(mach_task_self(),
                                         &task_bootstrap_port);

  if (init_result_ != KERN_SUCCESS)
    return;

  init_result_ = bootstrap_look_up(task_bootstrap_port,
                    const_cast<char*>(receive_port_name),
                    &send_port_);
}

//==============================================================================
MachPortSender::MachPortSender(mach_port_t send_port)
  : send_port_(send_port),
    init_result_(KERN_SUCCESS) {
}

//==============================================================================
kern_return_t MachPortSender::SendMessage(MachSendMessage &message,
                                          mach_msg_timeout_t timeout) {
  if (message.head.msgh_size == 0) {
    return KERN_INVALID_VALUE;    // just for safety -- never should occur
  };

  if (init_result_ != KERN_SUCCESS)
    return init_result_;

  message.head.msgh_remote_port = send_port_;

  kern_return_t result = mach_msg(&message.head,
                                  MACH_SEND_MSG | MACH_SEND_TIMEOUT,
                                  message.head.msgh_size,
                                  0,
                                  MACH_PORT_NULL,
                                  timeout,              // timeout in ms
                                  MACH_PORT_NULL);

  return result;
}

}  // namespace google_breakpad

{'repo_name': 'mozilla/positron', 'stars': '514', 'repo_language': 'None', 'file_name': 'moz.build', 'mime_type': 'text/plain', 'hash': 7638124452761413548, 'source_dataset': 'data'}


#! /usr/bin/env python

# Copyright (c) 2019 Uber Technologies, Inc.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import tensorflow as tf
from tf_plus import BatchNormalization, Lambda   # BN + Lambda layers are custom, rest are just from tf.layers
from tf_plus import Conv2D, MaxPooling2D, Flatten, Dense, UpSampling2D
from tf_plus import he_normal, relu
from tf_plus import Layers, SequentialNetwork, l2reg
from general.tfutil import tf_reshape_like
from CoordConv import AddCoords, CoordConv

Deconv = tf.layers.Conv2DTranspose
ReLu = Lambda(lambda xx: relu(xx))
LReLu = Lambda(lambda xx: lrelu(xx))
Softmax = Lambda(lambda xx: tf.nn.softmax(xx, axis=-1))
Tanh = Lambda(lambda xx: tf.nn.tanh(xx))
GlobalPooling = Lambda(lambda xx: tf.reduce_mean(xx,[1,2]))

class DeconvPainter(Layers):
    '''A Deconv net that paints an image as directed by x,y coord inputs '''

    def __init__(self, l2=0, x_dim=64, y_dim=64, fs=3, mul=1,
                 onthefly=True, use_mse_loss=False, use_sigm_loss=False):
        super(DeconvPainter, self).__init__()
        self.x_dim = x_dim
        self.y_dim = y_dim
        self.onthefly = onthefly
        self.use_mse_loss = use_mse_loss
        self.use_sigm_loss = use_sigm_loss

        with tf.variable_scope("model"):
            self.l('model', SequentialNetwork([
                   Lambda(lambda xx: tf.reshape(xx, [-1, 1, 1, 2])),
                   Deconv(64*mul, (fs,fs), (2,2), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                        # output_shape=[None, 2, 2, 64]
                   BatchNormalization(momentum=0.9, epsilon=1e-5),
                   ReLu,
                   Deconv(64*mul, (fs,fs), (2,2), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                       # output_shape=[None, 4, 4, 64]
                   BatchNormalization(momentum=0.9, epsilon=1e-5),
                   ReLu,
                   Deconv(64*mul, (fs,fs), (2,2), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                       # output_shape=[None, 8, 8, 64]
                   BatchNormalization(momentum=0.9, epsilon=1e-5),
                   ReLu,
                   Deconv(32*mul, (fs,fs), (2,2), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                       # output_shape=[None, 16, 16, 32]
                   BatchNormalization(momentum=0.9, epsilon=1e-5),
                   ReLu,
                   Deconv(32*mul, (fs,fs), (2,2), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                       # output_shape=[None, 32, 32, 32]
                   BatchNormalization(momentum=0.9, epsilon=1e-5),
                   ReLu,
                   Deconv(1, (fs,fs), (2,2), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                       # output_shape=[None, 64, 64, 1]
                   ], name='deconv_painter'))

        return

    def call(self, inputs):
        # Shapes:
        # input_coords: (batch, 2)
        # logits: (batch, x_dim, y_dim, 1)
        # logits_flat: (batch, x_dim*y_dim)

        input_coords = inputs[0]

        logits = self.model([input_coords])
        logits = tf.identity(logits, name='logits') # just to rename it

        logits_flat = Flatten()(logits)

        batch_size_tensor = tf.shape(input_coords)[0]  # get batch size

        prob_flat = tf.nn.softmax(logits_flat)
        prob = tf_reshape_like(prob_flat, logits, name='softmax_prob')

        pixelwise_prob_flat = tf.nn.sigmoid(logits_flat)
        pixelwise_prob = tf_reshape_like(pixelwise_prob_flat, logits, name='pixelwise_prob')

        if self.onthefly: # make labels on the fly
            indices = tf.reshape(tf.range(batch_size_tensor), [batch_size_tensor, 1])
            concat_indices = tf.concat([indices, input_coords], 1)
            labels_shape = [batch_size_tensor, self.x_dim, self.y_dim]
            labels = tf.sparse_to_dense(concat_indices, labels_shape, 1.)
            self.a('concat_indices', concat_indices)
        else:
            assert len(inputs) == 2, "Not on-the-fly, supply a target image"
            labels = inputs[1]

        labels_flat = Flatten()(labels)

        self.a('prob', prob)
        self.a('prob_flat', prob_flat)
        self.a('pixelwise_prob', pixelwise_prob)
        self.a('pixelwise_prob_flat', pixelwise_prob_flat)
        self.a('logits', logits)
        self.a('logits_flat', logits_flat)
        self.a('labels', labels)
        self.a('labels_flat', labels_flat)

        self.make_losses_and_metrics()
        return logits

    def make_losses_and_metrics(self):

        xe_loss = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.labels_flat))

        mse_loss = tf.reduce_mean(
                    tf.pow(self.logits_flat - self.labels_flat, 2))
        # Validated -- the same as above
        #mse_loss_tf = tf.losses.mean_squared_error(labels=self.labels_flat, predictions=self.logits_flat)
        #self.a('mse_loss_tf', mse_loss_tf, trackable=True)

        sigm_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.labels_flat))

        # regularizers
        reg_loss = tf.losses.get_regularization_loss()
        reg_losses = tf.losses.get_regularization_losses()

        argmax_prob = tf.argmax(self.prob_flat, 1)   # index in [0,64*64)
        # convert indices to 2D coordinates
        argmax_x = argmax_prob // self.x_dim
        argmax_y = argmax_prob % self.x_dim

        argmax_label = tf.argmax(self.labels_flat, 1)

        argmax_x_l = tf.floordiv(argmax_label, self.y_dim)
        argmax_y_l = tf.mod(argmax_label, self.y_dim)

        self.a('argmax_prob', argmax_prob)
        self.a('argmax_label', argmax_label)
        self.a('argmax_x', argmax_x)
        self.a('argmax_y', argmax_y)
        self.a('argmax_x_l', argmax_x_l)
        self.a('argmax_y_l', argmax_y_l)

        correct = tf.equal(argmax_prob, argmax_label)
        accuracy = tf.reduce_mean(tf.to_float(correct))
        eucl_dist = tf.reduce_mean(tf.sqrt(tf.to_float(tf.square(argmax_x-argmax_x_l) + tf.square(argmax_y-argmax_y_l))))
        manh_dist = tf.reduce_mean(tf.to_float(tf.abs(argmax_x-argmax_x_l) + tf.abs(argmax_y-argmax_y_l)))

        self.a('reg_losses', reg_losses)
        self.a('correct', correct)
        self.a('accuracy', accuracy, trackable=True)
        self.a('eucl_dist', eucl_dist, trackable=True)
        self.a('manh_dist', manh_dist, trackable=True)
        self.a('xe_loss', xe_loss, trackable=True)
        self.a('mse_loss', mse_loss, trackable=True)
        self.a('sigm_loss', sigm_loss, trackable=True)
        self.a('reg_loss', reg_loss, trackable=True)

        # calculate IOU, useful if input_label is an image

        painted = tf.round(self.pixelwise_prob_flat)  # 1 if > 0.5, 0 if <= 0.5
        n_painted = tf.reduce_mean(tf.reduce_sum(painted, 1))

        n_intersection = tf.reduce_sum(tf.multiply(painted, self.labels_flat), 1) # num of pixels in intersection

        n_union = tf.reduce_sum(tf.to_float(tf.logical_or(tf.cast(painted, tf.bool), tf.cast(self.labels_flat, tf.bool))), 1)

        self.a('painted', painted)
        self.a('n_intersection', n_intersection)
        self.a('n_union', n_union)

        intersect_pixs = tf.reduce_mean(n_intersection)
        iou = tf.reduce_mean(tf.div(n_intersection, n_union))

        self.a('painted_pixs', n_painted, trackable=True)
        self.a('intersect_pixs', intersect_pixs, trackable=True)
        self.a('iou', iou, trackable=True)

        if self.use_mse_loss:
            self.a('loss', mse_loss+reg_loss, trackable=True)
        elif self.use_sigm_loss:
            self.a('loss', sigm_loss+reg_loss, trackable=True)
        else:
            self.a('loss', xe_loss+reg_loss, trackable=True)

        return

class ConvImagePainter(Layers):
    '''A model net that paints a circle with one-hot center inputs'''

    def __init__(self, l2=0, fs=3, mul=1, use_sigm_loss=True,
                 use_mse_loss=False, version='working'):
        super(ConvImagePainter, self).__init__()
        self.use_sigm_loss = use_sigm_loss
        self.use_mse_loss = use_mse_loss
        assert version in ['simple', 'working', 'dilation'], "model version not supported"
        self.version = version

        if version == 'simple':
            net = build_simple_one_channel_onehot2image(l2, name='model')
            self.l('model', net)

        elif version == 'working':
            net = build_working_conv_onehot2image(l2, mul, fs, name='model')
            self.l('model', net)

        return

    def call(self, inputs):

        assert len(inputs) == 2, "model requires 2 tensors: input_1hot, input_images"
        input_1hot, labels = inputs[0], inputs[1]

        logits = self.model(input_1hot)
        logits = tf.identity(logits, name='logits') # just to rename it

        logits_flat = Flatten()(logits)

        # Shapes:
        # logits: (batch, x_dim, y_dim, 1)
        # logits_flat: (batch, x_dim*y_dim)

        pixelwise_prob_flat = tf.nn.sigmoid(logits_flat)
        pixelwise_prob = tf_reshape_like(pixelwise_prob_flat, logits, name='pixelwise_prob')

        labels_flat = Flatten()(labels)

        self.a('logits', logits)
        self.a('logits_flat', logits_flat)
        self.a('pixelwise_prob', pixelwise_prob)
        self.a('pixelwise_prob_flat', pixelwise_prob_flat)
        self.a('labels', labels)
        self.a('labels_flat', labels_flat)

        self.make_losses_and_metrics()
        return logits

    def make_losses_and_metrics(self):

        mse_loss = tf.reduce_mean(
                    tf.pow(self.logits_flat - self.labels_flat, 2))

        sigm_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.labels_flat))

        # regularizers
        reg_loss = tf.losses.get_regularization_loss()
        reg_losses = tf.losses.get_regularization_losses()

        self.a('mse_loss', mse_loss, trackable=True)
        self.a('sigm_loss', sigm_loss, trackable=True)
        self.a('reg_loss', reg_loss, trackable=True)

        # calculate IOU, useful if input_label is an image

        painted = tf.round(self.pixelwise_prob_flat)  # 1 if > 0.5, 0 if <= 0.5
        n_painted = tf.reduce_mean(tf.reduce_sum(painted, 1))

        n_intersection = tf.reduce_sum(tf.multiply(painted, self.labels_flat), 1) # num of pixels in intersection
        n_union = tf.reduce_sum(
                tf.to_float(tf.logical_or(tf.cast(painted, tf.bool),
                            tf.cast(self.labels_flat, tf.bool))),
                1)

        self.a('painted', painted)
        self.a('n_intersection', n_intersection)
        self.a('n_union', n_union)

        intersect_pixs = tf.reduce_mean(n_intersection)
        iou = tf.reduce_mean(tf.div(n_intersection, n_union))

        self.a('painted_pixs', n_painted, trackable=True)
        self.a('intersect_pixs', intersect_pixs, trackable=True)
        self.a('iou', iou, trackable=True)

        if self.use_mse_loss:
            self.a('loss', mse_loss+reg_loss, trackable=True)
        elif self.use_sigm_loss:
            self.a('loss', sigm_loss+reg_loss, trackable=True)
        else:
            raise ValueError('use either sigmoid or mse loss')

        return

class ConvRegressor(Layers):
    '''A model net that paints a circle with one-hot center inputs'''

    def __init__(self, l2=0, x_dim=64, y_dim=64,
                 mul=1, _type='conv_uniform'):
        self.type=_type

        super(ConvRegressor, self).__init__()
        include_r = False

        def coordconv_model():
        #from onehots to coordinate with coordinate augmentation at beginning

            return SequentialNetwork([
                AddCoords(x_dim=x_dim, y_dim=y_dim, with_r=include_r, skiptile=True), # (batch, 64, 64, 4 or 5)
                Conv2D(8, (1,1), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                Conv2D(8, (1,1), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                Conv2D(8, (1,1), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                Conv2D(8, (3,3), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                Conv2D(2, (3,3), padding='same',
                        kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                MaxPooling2D(pool_size=64,strides=64,padding='valid'),

                ])

        def big_conv2():

            return SequentialNetwork([
                Conv2D(16*mul, (5,5), padding='same',
                   kernel_initializer=he_normal, strides=2,kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(16*mul, (1,1), padding='same',strides=1,
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,

                BatchNormalization(momentum=0.9, epsilon=1e-5),
                Conv2D(16*mul, (3,3), padding='same',strides=1,
                   kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,

                Conv2D(16*mul, (3,3), padding='same',
                   kernel_initializer=he_normal, strides=2,kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(16*mul, (3,3), padding='same',
                       kernel_initializer=he_normal, strides=2,kernel_regularizer=l2reg(l2)),
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Conv2D(16*mul, (3,3), padding='same',strides=2,
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(16*mul, (1,1), padding='same',strides=1,
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(16*mul, (3,3), padding='same',strides=2,
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(2, (3,3), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                GlobalPooling #pool_size=-1,strides=1,padding='valid'),
                ])

        def big_conv():

            return SequentialNetwork([
                Conv2D(16*mul, (3,3), padding='same',
                   kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                MaxPooling2D(pool_size=2,strides=2,padding='valid'),
                Conv2D(16*mul, (3,3), padding='same',
                   kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                MaxPooling2D(pool_size=2,strides=2,padding='valid'),
                Conv2D(16*mul, (3,3), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                MaxPooling2D(pool_size=2,strides=2,padding='valid'),
                Conv2D(16*mul, (3,3), padding='same',
                       kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Flatten(),
                Dense(64),
                ReLu,
                Dense(2) #pool_size=-1,strides=1,padding='valid'),
                ])

        with tf.variable_scope("model"):
            if self.type=='conv_uniform':
                self.l('model',big_conv())
            elif self.type=='conv_quarant':
                self.l('model',big_conv2())
            elif self.type=='coordconv':
                self.l('model',coordconv_model())

        return

    def call(self, inputs):

        assert len(inputs) == 2, "model requires 2 tensors: input_images (onehots), target coordinates"
        input_images, labels = inputs[0], inputs[1]

        logits = self.model(input_images)
        logits_flat = Flatten()(logits)

        # Shapes:
        # logits: (batch, x_dim, y_dim, 1)
        # logits_flat: (batch, x_dim*y_dim)
        labels_flat = tf.cast(Flatten()(labels),tf.float32)

        self.a('logits', logits_flat)
        #self.a('logits_flat', logits_flat)
        self.a('labels', labels_flat)
        #self.a('labels_flat', labels_flat)

        self.make_losses_and_metrics()

        return logits

    def make_losses_and_metrics(self):

        mse_loss = tf.reduce_mean(
                    tf.square(self.logits - self.labels))

        sigm_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=self.logits, labels=self.labels))

        # regularizers
        reg_loss = tf.losses.get_regularization_loss()
        reg_losses = tf.losses.get_regularization_losses()

        eucl_dist = tf.reduce_mean(tf.sqrt(tf.reduce_sum(tf.to_float(tf.square((64.0*self.logits)-(64.0*self.labels))),-1)))
        self.a('eucl_dist', eucl_dist, trackable=True)
        #self.a('mse_dist', mse_loss, trackable=True)

        self.a('loss', mse_loss+reg_loss, trackable=True)
        return

class CoordConvPainter(Layers):
    '''A CoordConv that paints a pixel as directed by x,y coord inputs'''

    def __init__(self, l2=0, x_dim=64, y_dim=64, mul=1, include_r=False,
                 use_mse_loss=False, use_sigm_loss=False):
        super(CoordConvPainter, self).__init__()
        self.x_dim = x_dim
        self.y_dim = y_dim
        self.use_mse_loss = use_mse_loss
        self.use_sigm_loss = use_sigm_loss

        with tf.variable_scope("model"):
            self.l('coordconvprep', SequentialNetwork([
                Lambda(lambda xx: tf.reshape(xx, [-1, 1, 1, 2])),
                AddCoords(x_dim=x_dim, y_dim=y_dim, with_r=include_r, skiptile=False), # (batch, 64, 64, 4 or 5)
                ], name='coordconvprep'))

            self.l('model', SequentialNetwork([
                Conv2D(32*mul, (1,1), padding='valid',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(32*mul, (1,1), padding='valid',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(64*mul, (1,1), padding='valid',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(64*mul, (1,1), padding='valid',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(1, (1,1), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ], name='coordconv_painter'))
        return

    def call(self, inputs):
        # Shapes:
        # input_coords: (batch, 2)
        # logits: (batch, x_dim, y_dim, 1)
        # logits_flat: (batch, x_dim*y_dim)

        input_coords = inputs[0]

        prepped_coords = self.coordconvprep(input_coords)
        self.a('prepped_coords', prepped_coords)

        logits = self.model(prepped_coords)
        logits = tf.identity(logits, name='logits') # just to rename it

        logits_flat = Flatten()(logits)

        batch_size_tensor = tf.shape(input_coords)[0]  # get batch size

        prob_flat = tf.nn.softmax(logits_flat)
        prob = tf_reshape_like(prob_flat, logits, name='softmax_prob')

        pixelwise_prob_flat = tf.nn.sigmoid(logits_flat)
        pixelwise_prob = tf_reshape_like(pixelwise_prob_flat, logits, name='pixelwise_prob')

        assert len(inputs) == 2, "Not on-the-fly, supply a target image"
        labels = inputs[1]

        labels_flat = Flatten()(labels)

        self.a('prob', prob)
        self.a('prob_flat', prob_flat)
        self.a('pixelwise_prob', pixelwise_prob)
        self.a('pixelwise_prob_flat', pixelwise_prob_flat)
        self.a('logits', logits)
        self.a('logits_flat', logits_flat)
        self.a('labels', labels)
        self.a('labels_flat', labels_flat)

        self.make_losses_and_metrics()
        return logits

    def make_losses_and_metrics(self):

        xe_loss = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.labels_flat))

        mse_loss = tf.reduce_mean(
                    tf.pow(self.logits_flat - self.labels_flat, 2))

        sigm_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.labels_flat))

        # regularizers
        reg_loss = tf.losses.get_regularization_loss()
        reg_losses = tf.losses.get_regularization_losses()

        argmax_prob = tf.argmax(self.prob_flat, 1)   # index in [0,64*64)
        # convert indices to 2D coordinates
        argmax_x = argmax_prob // self.x_dim
        argmax_y = argmax_prob % self.x_dim

        argmax_label = tf.argmax(self.labels_flat, 1)

        argmax_x_l = tf.floordiv(argmax_label, self.y_dim)
        argmax_y_l = tf.mod(argmax_label, self.y_dim)

        self.a('argmax_prob', argmax_prob)
        self.a('argmax_label', argmax_label)
        self.a('argmax_x', argmax_x)
        self.a('argmax_y', argmax_y)
        self.a('argmax_x_l', argmax_x_l)
        self.a('argmax_y_l', argmax_y_l)

        correct = tf.equal(argmax_prob, argmax_label)
        accuracy = tf.reduce_mean(tf.to_float(correct))
        eucl_dist = tf.reduce_mean(tf.sqrt(tf.to_float(tf.square(argmax_x-argmax_x_l) + tf.square(argmax_y-argmax_y_l))))
        manh_dist = tf.reduce_mean(tf.to_float(tf.abs(argmax_x-argmax_x_l) + tf.abs(argmax_y-argmax_y_l)))

        self.a('reg_losses', reg_losses)
        self.a('correct', correct)
        self.a('accuracy', accuracy, trackable=True)
        self.a('eucl_dist', eucl_dist, trackable=True)
        self.a('manh_dist', manh_dist, trackable=True)
        self.a('xe_loss', xe_loss, trackable=True)
        self.a('mse_loss', mse_loss, trackable=True)
        self.a('sigm_loss', sigm_loss, trackable=True)
        self.a('reg_loss', reg_loss, trackable=True)

        # calculate IOU, useful if input_label is an image

        painted = tf.round(self.pixelwise_prob_flat)  # 1 if > 0.5, 0 if <= 0.5
        n_painted = tf.reduce_mean(tf.reduce_sum(painted, 1))

        n_intersection = tf.reduce_sum(tf.multiply(painted, self.labels_flat), 1) # num of pixels in intersection

        n_union = tf.reduce_sum(tf.to_float(tf.logical_or(tf.cast(painted, tf.bool), tf.cast(self.labels_flat, tf.bool))), 1)

        self.a('painted', painted)
        self.a('n_intersection', n_intersection)
        self.a('n_union', n_union)

        intersect_pixs = tf.reduce_mean(n_intersection)
        iou = tf.reduce_mean(tf.div(n_intersection, n_union))

        self.a('painted_pixs', n_painted, trackable=True)
        self.a('intersect_pixs', intersect_pixs, trackable=True)
        self.a('iou', iou, trackable=True)

        if self.use_mse_loss:
            self.a('loss', mse_loss+reg_loss, trackable=True)
        elif self.use_sigm_loss:
            self.a('loss', sigm_loss+reg_loss, trackable=True)
        else:
            self.a('loss', xe_loss+reg_loss, trackable=True)

        return

class UpsampleConvPainter(Layers):
    '''A upsample+conv model that paints a pixel as directed by x,y coord inputs'''

    def __init__(self, l2=0, x_dim=64, y_dim=64, fs=3, mul=1, coordconv=False,
                 include_r=False, use_mse_loss=False, use_sigm_loss=False):
        super(UpsampleConvPainter, self).__init__()
        self.x_dim = x_dim
        self.y_dim = y_dim
        self.use_mse_loss = use_mse_loss
        self.use_sigm_loss = use_sigm_loss

        with tf.variable_scope("model"):
            if coordconv:
                self.l('model', SequentialNetwork([
                       Lambda(lambda xx: tf.cast(xx, 'float32')),
                       Lambda(lambda xx: tf.reshape(xx, [-1, 1, 1, 2])),
                       UpSampling2D(size=(2,2)),
                           # output_shape=[None, 2, 2, 2]
                       CoordConv(2, 2, False, 64*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)
                           ),
                           # output_shape=[None, 2, 2, 4]
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                           # output_shape=[None, 2, 2, 64]
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 4, 4, 64]
                       AddCoords(x_dim=4, y_dim=4, with_r=include_r, skiptile=True),
                       Conv2D(64*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                           # output_shape=[None, 4, 4, 64]
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 8, 8, 64]
                       AddCoords(x_dim=8, y_dim=8, with_r=include_r, skiptile=True),
                       Conv2D(64*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                           # output_shape=[None, 8, 8, 64]
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 16, 16, 64]
                       AddCoords(x_dim=16, y_dim=16, with_r=include_r, skiptile=True),
                       Conv2D(32*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                           # output_shape=[None, 16, 16, 32]
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 32, 32, 32]
                       AddCoords(x_dim=32, y_dim=32, with_r=include_r, skiptile=True),
                       Conv2D(32*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                           # output_shape=[None, 32, 32, 32]
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 64, 64, 32]
                       AddCoords(x_dim=64, y_dim=64, with_r=include_r, skiptile=True),
                       Conv2D(1, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                           # output_shape=[None, 64, 64, 1]
                       ], name='upsample_coordconv_painter'))
            else:
                self.l('model', SequentialNetwork([
                       Lambda(lambda xx: tf.cast(xx, 'float32')),
                       Lambda(lambda xx: tf.reshape(xx, [-1, 1, 1, 2])),
                       UpSampling2D(size=(2,2)),
                           # output_shape=[None, 2, 2, 2]
                       Conv2D(64*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                           # output_shape=[None, 2, 2, 64]
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 4, 4, 64]
                       Conv2D(64*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                           # output_shape=[None, 4, 4, 64]
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 8, 8, 64]
                       Conv2D(64*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                           # output_shape=[None, 8, 8, 64]
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 16, 16, 64]
                       Conv2D(32*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                           # output_shape=[None, 16, 16, 32]
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 32, 32, 32]
                       Conv2D(32*mul, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                           # output_shape=[None, 32, 32, 32]
                       BatchNormalization(momentum=0.9, epsilon=1e-5),
                       ReLu,
                       UpSampling2D(size=(2,2)),  # output_shape=[None, 64, 64, 32]
                       Conv2D(1, (fs,fs), padding='same',
                           kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                           # output_shape=[None, 64, 64, 1]
                       ], name='upsample_conv_painter'))
        return

    def call(self, inputs):
        # Shapes:
        # input_coords: (batch, 2)
        # logits: (batch, x_dim, y_dim, 1)
        # logits_flat: (batch, x_dim*y_dim)

        input_coords = inputs[0]

        logits = self.model([input_coords])
        logits = tf.identity(logits, name='logits') # just to rename it

        logits_flat = Flatten()(logits)

        batch_size_tensor = tf.shape(input_coords)[0]  # get batch size

        prob_flat = tf.nn.softmax(logits_flat)
        prob = tf_reshape_like(prob_flat, logits, name='softmax_prob')

        pixelwise_prob_flat = tf.nn.sigmoid(logits_flat)
        pixelwise_prob = tf_reshape_like(pixelwise_prob_flat, logits, name='pixelwise_prob')

        assert len(inputs) == 2, "Not on-the-fly, supply a target image"
        labels = inputs[1]

        labels_flat = Flatten()(labels)

        self.a('prob', prob)
        self.a('prob_flat', prob_flat)
        self.a('pixelwise_prob', pixelwise_prob)
        self.a('pixelwise_prob_flat', pixelwise_prob_flat)
        self.a('logits', logits)
        self.a('logits_flat', logits_flat)
        self.a('labels', labels)
        self.a('labels_flat', labels_flat)

        self.make_losses_and_metrics()
        return logits

    def make_losses_and_metrics(self):

        xe_loss = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.labels_flat))

        mse_loss = tf.reduce_mean(
                    tf.pow(self.logits_flat - self.labels_flat, 2))

        sigm_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.labels_flat))

        # regularizers
        reg_loss = tf.losses.get_regularization_loss()
        reg_losses = tf.losses.get_regularization_losses()

        argmax_prob = tf.argmax(self.prob_flat, 1)   # index in [0,64*64)
        # convert indices to 2D coordinates
        argmax_x = argmax_prob // self.x_dim
        argmax_y = argmax_prob % self.x_dim

        argmax_label = tf.argmax(self.labels_flat, 1)

        argmax_x_l = tf.floordiv(argmax_label, self.y_dim)
        argmax_y_l = tf.mod(argmax_label, self.y_dim)

        self.a('argmax_prob', argmax_prob)
        self.a('argmax_label', argmax_label)
        self.a('argmax_x', argmax_x)
        self.a('argmax_y', argmax_y)
        self.a('argmax_x_l', argmax_x_l)
        self.a('argmax_y_l', argmax_y_l)

        correct = tf.equal(argmax_prob, argmax_label)
        accuracy = tf.reduce_mean(tf.to_float(correct))
        eucl_dist = tf.reduce_mean(tf.sqrt(tf.to_float(tf.square(argmax_x-argmax_x_l) + tf.square(argmax_y-argmax_y_l))))
        manh_dist = tf.reduce_mean(tf.to_float(tf.abs(argmax_x-argmax_x_l) + tf.abs(argmax_y-argmax_y_l)))

        self.a('reg_losses', reg_losses)
        self.a('correct', correct)
        self.a('accuracy', accuracy, trackable=True)
        self.a('eucl_dist', eucl_dist, trackable=True)
        self.a('manh_dist', manh_dist, trackable=True)
        self.a('xe_loss', xe_loss, trackable=True)
        self.a('mse_loss', mse_loss, trackable=True)
        self.a('sigm_loss', sigm_loss, trackable=True)
        self.a('reg_loss', reg_loss, trackable=True)

        # calculate IOU, useful if input_label is an image

        painted = tf.round(self.pixelwise_prob_flat)  # 1 if > 0.5, 0 if <= 0.5
        n_painted = tf.reduce_mean(tf.reduce_sum(painted, 1))

        n_intersection = tf.reduce_sum(tf.multiply(painted, self.labels_flat), 1) # num of pixels in intersection

        n_union = tf.reduce_sum(tf.to_float(tf.logical_or(tf.cast(painted, tf.bool), tf.cast(self.labels_flat, tf.bool))), 1)

        self.a('painted', painted)
        self.a('n_intersection', n_intersection)
        self.a('n_union', n_union)

        intersect_pixs = tf.reduce_mean(n_intersection)
        iou = tf.reduce_mean(tf.div(n_intersection, n_union))

        self.a('painted_pixs', n_painted, trackable=True)
        self.a('intersect_pixs', intersect_pixs, trackable=True)
        self.a('iou', iou, trackable=True)

        if self.use_mse_loss:
            self.a('loss', mse_loss+reg_loss, trackable=True)
        elif self.use_sigm_loss:
            self.a('loss', sigm_loss+reg_loss, trackable=True)
        else:
            self.a('loss', xe_loss+reg_loss, trackable=True)

        return

class CoordConvImagePainter(Layers):
    '''A CoordConv that paints an image  as directed by x,y coord inputs'''

    def __init__(self, l2=0, fs=3, x_dim=64, y_dim=64, mul=1, include_r=False,
            use_mse_loss=False, use_sigm_loss=True, interm_loss=None,
            no_softmax=False, version='working'):
        super(CoordConvImagePainter, self).__init__()
        self.x_dim = x_dim
        self.y_dim = y_dim
        self.use_mse_loss = use_mse_loss
        self.use_sigm_loss = use_sigm_loss
        self.interm_loss = interm_loss

        self.l('coordconvprep', SequentialNetwork([
            Lambda(lambda xx: tf.reshape(xx, [-1, 1, 1, 2])),
            AddCoords(x_dim=x_dim, y_dim=y_dim, with_r=include_r, skiptile=False), # (batch, 64, 64, 4 or 5)
            ], name='coordconvprep'))

        self.l('coordconvmodel', SequentialNetwork([
            Conv2D(32, (1,1), padding='valid',
                kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
            ReLu,
            Conv2D(32, (1,1), padding='valid',
                kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
            ReLu,
            Conv2D(64, (1,1), padding='valid',
                kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
            ReLu,
            Conv2D(64, (1,1), padding='valid',
                kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
            ReLu,
            Conv2D(1, (1,1), padding='same',
                kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
            ], name='coordconvmodel'))

        self.l('sharpen', SequentialNetwork([
            Flatten(), # (batch, -1)
            Softmax,
            Lambda(lambda xx: tf.reshape(xx, [-1, x_dim, y_dim, 1])),
            ], name='sharpen'))

        if version == 'simple':
            net = build_simple_one_channel_onehot2image(l2, name='convmodel')
            self.l('convmodel', net)

        elif version == 'working':
            net = build_working_conv_onehot2image(l2, mul, fs, name='convmodel')
            self.l('convmodel', net)

        if no_softmax:
            self.l('model', SequentialNetwork([
                ('coordconvprep', self.coordconvprep),
                ('coordconvmodel', self.coordconvmodel),
                ('convmodel', self.convmodel)
                ], name='model'))
        else:
            self.l('model', SequentialNetwork([
                ('coordconvprep', self.coordconvprep),
                ('coordconvmodel', self.coordconvmodel),
                ('sharpen', self.sharpen),
                ('convmodel', self.convmodel)
                ], name='model'))

        return

    def call(self, inputs):
        # Shapes:
        # input_coords: (batch, 2)
        # logits: (batch, x_dim, y_dim, 1)
        # logits_flat: (batch, x_dim*y_dim)

        if len(inputs) == 2:
            input_coords, input_images = inputs[0], inputs[1]
        elif len(inputs) == 3:
            input_coords, input_1hot, input_images = inputs[0], inputs[1], inputs[2]
        else:
            raise ValueError('model requires either 2 or 3 tensors: input_coords, (input_1hot,) input_images')

        prepped_coords = self.coordconvprep(input_coords)
        center_logits = self.coordconvmodel(prepped_coords)
        center_logits = tf.identity(center_logits, name='center_logits') # just to rename it
        center_logits_flat = Flatten()(center_logits)
        sharpened_logits = self.sharpen(center_logits)
        sharpened_logits = tf.identity(sharpened_logits, name='sharpened_logits') # just to rename it

        self.a('prepped_coords', prepped_coords)
        self.a('center_logits', center_logits)
        self.a('center_logits_flat', center_logits_flat)
        self.a('sharpened_logits', sharpened_logits)

        logits = self.model(input_coords)
        #logits = self.convmodel(input_1hot) # HACK to see if second part of the model works
        logits = tf.identity(logits, name='logits') # just to rename it

        logits_flat = Flatten()(logits)

        center_prob = tf.identity(sharpened_logits, name='prob') # just to rename it
        center_prob_flat = Flatten()(center_prob)

        pixelwise_prob_flat = tf.nn.sigmoid(logits_flat)
        pixelwise_prob = tf_reshape_like(pixelwise_prob_flat, logits, name='pixelwise_prob')

        images_flat = Flatten()(input_images)

        self.a('center_prob', center_prob)
        self.a('center_prob_flat', center_prob_flat)
        self.a('pixelwise_prob', pixelwise_prob)
        self.a('pixelwise_prob_flat', pixelwise_prob_flat)
        self.a('logits', logits)
        self.a('logits_flat', logits_flat)
        self.a('images_flat', images_flat)

        if 'input_1hot' in locals():
            onehot_flat = Flatten()(input_1hot)
            self.a('onehot_flat', onehot_flat)

        self.make_losses_and_metrics()
        return logits

    def make_losses_and_metrics(self):

        # intermediate loss
        if hasattr(self, 'onehot_flat'):
            interm_softmax_loss = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits(
                    logits=self.center_logits_flat, labels=self.onehot_flat))
            interm_mse_loss = tf.reduce_mean(
                    tf.pow(self.center_logits_flat - self.onehot_flat, 2))
            self.a('interm_softmax_loss', interm_softmax_loss, trackable=True)
            self.a('interm_mse_loss', interm_mse_loss, trackable=True)

        # losses that have to do with only final images
        mse_loss = tf.reduce_mean(
                    tf.pow(self.logits_flat - self.images_flat, 2))

        sigm_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.images_flat))

        # regularizers
        reg_loss = tf.losses.get_regularization_loss()
        reg_losses = tf.losses.get_regularization_losses()

        argmax_prob = tf.argmax(self.center_prob_flat, 1)   # index in [0,64*64)
        ## convert indices to 2D coordinates
        argmax_x = argmax_prob // self.x_dim
        argmax_y = argmax_prob % self.x_dim
        self.a('argmax_prob', argmax_prob)
        self.a('argmax_x', argmax_x)
        self.a('argmax_y', argmax_y)

        if hasattr(self, 'onehot_flat'):
            argmax_label = tf.argmax(self.onehot_flat, 1)
            argmax_x_l = tf.floordiv(argmax_label, self.y_dim)
            argmax_y_l = tf.mod(argmax_label, self.y_dim)
            self.a('argmax_label', argmax_label)
            self.a('argmax_x_l', argmax_x_l)
            self.a('argmax_y_l', argmax_y_l)

            correct = tf.equal(argmax_prob, argmax_label)
            self.a('correct', correct)

            accuracy = tf.reduce_mean(tf.to_float(correct))
            eucl_dist = tf.reduce_mean(tf.sqrt(tf.to_float(tf.square(argmax_x-argmax_x_l) + tf.square(argmax_y-argmax_y_l))))
            manh_dist = tf.reduce_mean(tf.to_float(tf.abs(argmax_x-argmax_x_l) + tf.abs(argmax_y-argmax_y_l)))
            self.a('accuracy', accuracy, trackable=True)
            self.a('eucl_dist', eucl_dist, trackable=True)
            self.a('manh_dist', manh_dist, trackable=True)

        self.a('reg_losses', reg_losses)
        self.a('mse_loss', mse_loss, trackable=True)
        self.a('sigm_loss', sigm_loss, trackable=True)
        self.a('reg_loss', reg_loss, trackable=True)

        # calculate IOU, useful if input_label is an image

        painted = tf.round(self.pixelwise_prob_flat)  # 1 if > 0.5, 0 if <= 0.5
        n_painted = tf.reduce_mean(tf.reduce_sum(painted, 1))

        n_intersection = tf.reduce_sum(tf.multiply(painted, self.images_flat), 1) # num of pixels in intersection
        n_union = tf.reduce_sum(
                tf.to_float(tf.logical_or(tf.cast(painted, tf.bool),
                            tf.cast(self.images_flat, tf.bool))),
                1)

        self.a('painted', painted)
        self.a('n_intersection', n_intersection)
        self.a('n_union', n_union)

        intersect_pixs = tf.reduce_mean(n_intersection)
        iou = tf.reduce_mean(tf.div(n_intersection, n_union))

        self.a('painted_pixs', n_painted, trackable=True)
        self.a('intersect_pixs', intersect_pixs, trackable=True)
        self.a('iou', iou, trackable=True)

        if self.use_mse_loss:
            loss = mse_loss+reg_loss
        elif self.use_sigm_loss:
            loss = sigm_loss+reg_loss
        else:
            raise ValueError('use either sigmoid or mse loss')

        if self.interm_loss is not None:
            if self.interm_loss == 'mse':
                loss += interm_mse_loss
            elif self.interm_loss == 'softmax':
                loss += interm_softmax_loss
            else:
                raise ValueError('Support only `mse` or `softmax` intermediate loss')

        self.a('loss', loss, trackable=True)

        return

class DeconvBottleneckPainter(Layers):
    '''
    Like DeconvPainter but squeeze to 1 channel in between.
    Option to either sharpen that channel or not,
    with enforced loss or with softmax layer
    '''

    def __init__(self, l2=0, x_dim=64, y_dim=64, fs=3, mul=1,
                 use_mse_loss=False, use_sigm_loss=False,
                 interm_loss=None, no_softmax=False,
                 version='working'):
        super(DeconvBottleneckPainter, self).__init__()
        self.x_dim = x_dim
        self.y_dim = y_dim
        self.use_mse_loss = use_mse_loss
        self.use_sigm_loss = use_sigm_loss
        self.interm_loss = interm_loss

        net = build_deconv_coords2image(l2, mul, fs, name='coords2center')
        self.l('coords2center', net)

        self.l('sharpen', SequentialNetwork([
                Flatten(), # (batch, -1)
                Softmax,
                Lambda(lambda xx: tf.reshape(xx, [-1, x_dim, y_dim, 1])),
                ], name="sharpen"))

        if version == 'simple':
            net = build_simple_one_channel_onehot2image(l2, name='center2image')
            self.l('center2image', net)

        elif version == 'working':
            net = build_working_conv_onehot2image(l2, mul, fs, name='center2image')
            self.l('center2image', net)

            if no_softmax:
                self.l('model', SequentialNetwork([
                        ('coords2center', self.coords2center),
                        ('center2image', self.center2image)
                        ], name='model'))
            else:
                self.l('model', SequentialNetwork([
                        ('coords2center', self.coords2center),
                        ('sharpen', self.sharpen),
                        ('center2image', self.center2image)
                        ], name='model'))

        return

    def call(self, inputs):

        if len(inputs) == 2:
            input_coords, input_images = inputs[0], inputs[1]
        elif len(inputs) == 3:
            input_coords, input_1hot, input_images = inputs[0], inputs[1], inputs[2]
        else:
            raise ValueError('model requires either 2 or 3 tensors: input_coords, (input_1hot,) input_images')

        center_logits = self.coords2center(input_coords)
        center_logits = tf.identity(center_logits, name='center_logits') # just to rename it
        center_logits_flat = Flatten()(center_logits)
        sharpened_logits = self.sharpen(center_logits)
        sharpened_logits = tf.identity(sharpened_logits, name='sharpened_logits') # just to rename it

        self.a('center_logits', center_logits)
        self.a('center_logits_flat', center_logits_flat)
        self.a('sharpened_logits', sharpened_logits)

        logits = self.model(input_coords)
        logits = tf.identity(logits, name='logits') # just to rename it

        logits_flat = Flatten()(logits)

        center_prob = tf.identity(sharpened_logits, name='prob') # just to rename it
        center_prob_flat = Flatten()(center_prob)

        pixelwise_prob_flat = tf.nn.sigmoid(logits_flat)
        pixelwise_prob = tf_reshape_like(pixelwise_prob_flat, logits, name='pixelwise_prob')

        images_flat = Flatten()(input_images)

        self.a('center_prob', center_prob)
        self.a('center_prob_flat', center_prob_flat)
        self.a('pixelwise_prob', pixelwise_prob)
        self.a('pixelwise_prob_flat', pixelwise_prob_flat)
        self.a('logits', logits)
        self.a('logits_flat', logits_flat)
        self.a('images_flat', images_flat)

        if 'input_1hot' in locals():
            onehot_flat = Flatten()(input_1hot)
            self.a('onehot_flat', onehot_flat)

        self.make_losses_and_metrics()

        return logits

    def make_losses_and_metrics(self):
        # intermediate loss
        if hasattr(self, 'onehot_flat'):
            interm_softmax_loss = tf.reduce_mean(
                tf.nn.softmax_cross_entropy_with_logits(
                    logits=self.center_logits_flat, labels=self.onehot_flat))
            interm_mse_loss = tf.reduce_mean(
                    tf.pow(self.center_logits_flat - self.onehot_flat, 2))
            self.a('interm_softmax_loss', interm_softmax_loss, trackable=True)
            self.a('interm_mse_loss', interm_mse_loss, trackable=True)

        # losses that have to do with only final images
        mse_loss = tf.reduce_mean(
                    tf.pow(self.logits_flat - self.images_flat, 2))

        sigm_loss = tf.reduce_mean(
                tf.nn.sigmoid_cross_entropy_with_logits(
                    logits=self.logits_flat, labels=self.images_flat))

        # regularizers
        reg_loss = tf.losses.get_regularization_loss()
        reg_losses = tf.losses.get_regularization_losses()

        argmax_prob = tf.argmax(self.center_prob_flat, 1)   # index in [0,64*64)
        ## convert indices to 2D coordinates
        argmax_x = argmax_prob // self.x_dim
        argmax_y = argmax_prob % self.x_dim
        self.a('argmax_prob', argmax_prob)
        self.a('argmax_x', argmax_x)
        self.a('argmax_y', argmax_y)

        if hasattr(self, 'onehot_flat'):
            argmax_label = tf.argmax(self.onehot_flat, 1)
            argmax_x_l = tf.floordiv(argmax_label, self.y_dim)
            argmax_y_l = tf.mod(argmax_label, self.y_dim)
            self.a('argmax_label', argmax_label)
            self.a('argmax_x_l', argmax_x_l)
            self.a('argmax_y_l', argmax_y_l)

            correct = tf.equal(argmax_prob, argmax_label)
            self.a('correct', correct)

            accuracy = tf.reduce_mean(tf.to_float(correct))
            eucl_dist = tf.reduce_mean(tf.sqrt(tf.to_float(tf.square(argmax_x-argmax_x_l) + tf.square(argmax_y-argmax_y_l))))
            manh_dist = tf.reduce_mean(tf.to_float(tf.abs(argmax_x-argmax_x_l) + tf.abs(argmax_y-argmax_y_l)))
            self.a('accuracy', accuracy, trackable=True)
            self.a('eucl_dist', eucl_dist, trackable=True)
            self.a('manh_dist', manh_dist, trackable=True)

        self.a('reg_losses', reg_losses)
        self.a('mse_loss', mse_loss, trackable=True)
        self.a('sigm_loss', sigm_loss, trackable=True)
        self.a('reg_loss', reg_loss, trackable=True)

        # calculate IOU, useful if input_label is an image

        painted = tf.round(self.pixelwise_prob_flat)  # 1 if > 0.5, 0 if <= 0.5
        n_painted = tf.reduce_mean(tf.reduce_sum(painted, 1))

        n_intersection = tf.reduce_sum(tf.multiply(painted, self.images_flat), 1) # num of pixels in intersection
        n_union = tf.reduce_sum(
                tf.to_float(tf.logical_or(tf.cast(painted, tf.bool),
                            tf.cast(self.images_flat, tf.bool))),
                1)

        self.a('painted', painted)
        self.a('n_intersection', n_intersection)
        self.a('n_union', n_union)

        intersect_pixs = tf.reduce_mean(n_intersection)
        iou = tf.reduce_mean(tf.div(n_intersection, n_union))

        self.a('painted_pixs', n_painted, trackable=True)
        self.a('intersect_pixs', intersect_pixs, trackable=True)
        self.a('iou', iou, trackable=True)

        if self.use_mse_loss:
            loss = mse_loss+reg_loss
        elif self.use_sigm_loss:
            loss = sigm_loss+reg_loss
        else:
            raise ValueError('use either sigmoid or mse loss')

        if self.interm_loss is not None:
            if self.interm_loss == 'mse':
                loss += interm_mse_loss
            elif self.interm_loss == 'softmax':
                loss += interm_softmax_loss
            else:
                raise ValueError('Support only `mse` or `softmax` intermediate loss')

        self.a('loss', loss, trackable=True)
        return

def lrelu(x, leak=0.2, name="lrelu"):
    return tf.maximum(x, leak*x)

def build_working_conv_onehot2image(l2, mul, fs, name=''):
    net = SequentialNetwork([
                Conv2D(8*mul, (fs,fs), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                #BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Conv2D(8*mul, (fs,fs), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                #BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Conv2D(16*mul, (fs,fs), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                #BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Conv2D(16*mul, (fs,fs), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                #BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Conv2D(1, (fs,fs), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ], name=name)
    return net

class CoordConvGAN(Layers):
    '''basically DCGAN64 structure, enhanced with coordconv-like coordinate inputs'''

    def __init__(self, l2=0, x_dim=64, y_dim=64, cout=1,
                 add_r=False, coords_in_g=False, coords_in_d=False):

        super(CoordConvGAN, self).__init__()
        #self.l2 = l2
        #self.x_dim = x_dim
        #self.y_dim = y_dim
        #self.add_r = add_r
        self.coords_in_g = coords_in_g
        self.coords_in_d = coords_in_d

        with tf.variable_scope("generator"):
            self.l('coordconvprep_g', SequentialNetwork([
                Lambda(lambda xx: tf.expand_dims(tf.expand_dims(xx,1),1)),  # (batch, z_dim) -> (batch, 1, 1, z_dim)
                AddCoords(x_dim=x_dim, y_dim=y_dim, with_r=add_r, skiptile=False), # (batch, 64, 64, 4 or 5)
                ], name='coordconvprep_g'))

            self.l('coordconv_generator', SequentialNetwork([
                Conv2D(8*64, (1,1), padding='valid',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(64*4, (1,1), padding='valid',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(64*4, (1,1), padding='valid',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(64*2, (1,1), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(64*1, (1,1), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(64*1, (3,3), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(64*1, (3,3), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ReLu,
                Conv2D(cout, (1,1), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                ], name='coordconv_generator'))

            self.l('deconv_generator', SequentialNetwork([
                Dense(4*4*8*64),
                Lambda(lambda xx: tf.reshape(xx, [-1, 4, 4, 8*64])),
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(64*4, (5,5), (2,2), padding='same'),          # output_shape=[None, 8, 8, 512]
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(64*2, (5,5), (2,2), padding='same'),          # output_shape=[None, 16, 16, 256]
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(64*1, (5,5), (2,2), padding='same'),          # output_shape=[None, 32, 32, 128]
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(cout, (5,5), (2,2), padding='same'),            # output_shape=[None, 64, 64, 3]
                Tanh
                ], name='deconv_generator'))

        with tf.variable_scope("discriminator"):

            self.l('coordconvprep_d', SequentialNetwork([
                AddCoords(x_dim=x_dim, y_dim=y_dim, with_r=add_r, skiptile=True), # (batch, 64, 64, 4 or 5)
                ], name='coordconvprep_d'))

            self.l('discriminator_minus_last', SequentialNetwork([
                Conv2D(64, (5,5), 2, padding='same', activation=lrelu),
                Conv2D(128, (5,5), 2, padding='same'),
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                LReLu,
                Conv2D(256, (5,5), 2, padding='same'),
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                LReLu,
                Conv2D(512, (5,5), 2, padding='same'),
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                LReLu,
                Flatten(),
                #Dense(1)
                ], name='discriminator_minus_last'))

            self.l('discriminator', SequentialNetwork(
                [self.discriminator_minus_last,
                Dense(1)],
                name='discriminator'))

    def call(self, inputs, feature_matching_loss=False, feature_match_loss_weight=1.0):

        assert len(inputs) == 2+(feature_matching_loss), \
                'inputs: images, noises, (images2 if feature matching)'

        input_x, input_z = inputs[0], inputs[1]

        if feature_matching_loss:
            input_x2 = inputs[2]

        #self.build_model()

        # call generator
        if self.coords_in_g:
            enhanced_z = self.coordconvprep_g(input_z)
            g_out = self.coordconv_generator(enhanced_z)
            self.a('enhanced_z', enhanced_z)
        else:
            g_out = self.deconv_generator(input_z)

        self.a('g_out', g_out)

        if self.coords_in_g:
            self.a('fake_images', Tanh(g_out))
        else:
            self.a('fake_images', g_out)

        # call discriminator
        if self.coords_in_d:
            enhanced_img = self.coordconvprep_d(input_x)
            enhanced_img_fake = self.coordconvprep_d(g_out)
            d_real_logits = self.discriminator(enhanced_img)
            d_fake_logits = self.discriminator(enhanced_img_fake)

            if feature_matching_loss:
                enhanced_img2 = self.coordconvprep_d(input_x2)
                d_real_features = self.discriminator_minus_last(enhanced_img2)
                d_fake_features = self.discriminator_minus_last(enhanced_img_fake)
        else:
            d_real_logits = self.discriminator(input_x)
            d_fake_logits = self.discriminator(g_out)
            if feature_matching_loss:
                d_real_features = self.discriminator_minus_last(input_x2)
                d_fake_features = self.discriminator_minus_last(g_out)

        self.a('d_fake_logits', d_fake_logits)
        self.a('d_real_logits', d_real_logits)

        # get loss for discriminator
        d_loss_real = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=d_real_logits, labels=tf.ones_like(d_real_logits)))
        d_loss_fake = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=d_fake_logits, labels=tf.zeros_like(d_fake_logits)))

        self.a('d_loss_real', d_loss_real, trackable=True)
        self.a('d_loss_fake', d_loss_fake, trackable=True)

        # get loss for generator
        g_loss_basic = tf.reduce_mean(
            tf.nn.sigmoid_cross_entropy_with_logits(logits=d_fake_logits, labels=tf.ones_like(d_fake_logits)))
        self.a('g_loss_basic', g_loss_basic, trackable=True)

        # regularization loss
        reg_loss = tf.losses.get_regularization_loss()
        reg_losses = tf.losses.get_regularization_losses()
        self.a('reg_losses', reg_losses)
        self.a('reg_loss', reg_loss, trackable=True)

        if feature_matching_loss:
            g_loss_feture_match = tf.reduce_mean(tf.abs(tf.subtract(d_real_features, d_fake_features)))
            self.a('g_loss_feture_match', g_loss_feture_match, trackable=True)

            self.a('g_loss', g_loss_feture_match * feature_match_loss_weight + g_loss_basic + reg_loss, trackable=True)
            self.a('d_loss', d_loss_real + d_loss_fake, trackable=True)

        else:
            self.a('g_loss', g_loss_basic + reg_loss, trackable=True)
            self.a('d_loss', d_loss_real + d_loss_fake, trackable=True)

        # correct rate for discriminator
        pred_correct_real = tf.greater(d_real_logits, tf.zeros_like(d_real_logits))
        correct_real = tf.reduce_mean(tf.to_float(pred_correct_real))

        pred_correct_fake = tf.less(d_fake_logits, tf.zeros_like(d_fake_logits))
        correct_fake = tf.reduce_mean(tf.to_float(pred_correct_fake))

        self.a('correct_real', correct_real, trackable=True)
        self.a('correct_fake', correct_fake, trackable=True)

        return g_out

def build_simple_one_channel_onehot2image(l2, name=''):
    net = SequentialNetwork([
                Conv2D(1, (9,9), padding='same',
                    #kernel_initializer=he_normal,
                    kernel_initializer=tf.ones_initializer(),
                    bias_initializer=tf.constant_initializer([0.]),
                    kernel_regularizer=l2reg(l2)),
                ], name=name)
    return net

def build_deconv_coords2image(l2, mul, fs, name=''):
    net = SequentialNetwork([
                Lambda(lambda xx: tf.cast(xx, 'float32')),
                Lambda(lambda xx: tf.reshape(xx, [-1, 1, 1, 2])),
                Deconv(64*mul, (fs,fs), (2,2), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                     # output_shape=[None, 2, 2, 64]
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(64*mul, (fs,fs), (2,2), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                    # output_shape=[None, 4, 4, 64]
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(64*mul, (fs,fs), (2,2), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                    # output_shape=[None, 8, 8, 64]
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(32*mul, (fs,fs), (2,2), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                    # output_shape=[None, 16, 16, 32]
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(32*mul, (fs,fs), (2,2), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                    # output_shape=[None, 32, 32, 32]
                BatchNormalization(momentum=0.9, epsilon=1e-5),
                ReLu,
                Deconv(1, (fs,fs), (2,2), padding='same',
                    kernel_initializer=he_normal, kernel_regularizer=l2reg(l2)),
                    # output_shape=[None, 64, 64, 1]
                ], name=name)
    return net

{'repo_name': 'uber-research/CoordConv', 'stars': '117', 'repo_language': 'Python', 'file_name': 'loader.py', 'mime_type': 'text/plain', 'hash': 1801674546089283015, 'source_dataset': 'data'}